1
Demo不完整的類型,而訪問模板派生類(CRTP)」靜態功能,而這樣做SFINAE
template <typename T>
struct A {
void mem_func() {
// This works!!! Can access static_func<int>();
std::cout << T::template static_func<int>() << '\n';
}
template <typename K>
static constexpr bool static_func() { return true; }
template <typename T1>
typename std::enable_if<T::template static_func<T1>(), void> sfinae_func() {
// This breaks!!! T is an incomplete type??
std::cout << "This fails" <<'\n';
}
};
struct B : A<B> {
};
在這裏,我不知道爲什麼T :: static_func在成員函數foo工作(),但是當我去實例化sfinae_func,T :: static_func不可訪問,因爲T是不完整的!怎麼會這樣??
後續是這樣的: 如果標準限制我訪問enable_if中的T :: static_func,有沒有一種解決方法可以在需要時用T :: static_func隱藏A :: static_funct?
template <typename T1>
typename std::enable_if<T::template static_func<T1>(), void> sfinae_func()
這裏需要什麼工作要做,以使有電話,如果T的static_func使得T的static_func隱藏着的static_func?
你能解釋一下爲什麼會發生這種錯誤發生的呢? – themagicalyang
通常訪問依賴類型的memebers的方法應在實例化時訪問。問題在於'T'不是依賴類型的'sfinae_func'方法,因此它可能被編譯器感知,因爲它應該在創建時立即被訪問,並且在那時B不從A繼承 - 是不完整的編譯器實際上建議)... –
@他們的身體沒有實例化,直到使用;即使不使用簽名也是如此。 – Yakk