在执行 SFINAE 时访问模板派生 class (CRTP) 的静态函数时类型不完整

Incomplete type while accessing templated derived class (CRTP)' static function while doing SFINAE

Demo

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 隐藏 A 的 static_func,需要做什么才能启用?

要解决此问题,您可以通过使 T of sfinae_func 附加模板参数的默认值来延迟类型实例化,例如如下(哦,别忘了使用 std::enable_if 的内部类型来实际执行 sfinae):

#include <iostream>
template <typename T>
struct A {
    void mem_func() {
        std::cout << T::template static_func<int>() << '\n';
    }
    template <typename K>
    static constexpr bool static_func() { return true; }

    template <typename T1, typename TT = T>
    typename std::enable_if<TT::template static_func<T1>(), void>::type sfinae_func() {
        std::cout << "This fails" <<'\n';
    }
};

struct B : A<B> {
};
int main() {
    B a;
    a.mem_func();
    a.sfinae_func<int>();
}

[live demo]