C++ 模板:未应用SFINAE专门化
至少我认为,下面的代码片段是应用于专门化的一个尽可能直接的例子 最后一行是主要点,也是发生故障的地方。专门化的C++ 模板:未应用SFINAE专门化,c++,templates,sfinae,specialization,C++,Templates,Sfinae,Specialization,至少我认为,下面的代码片段是应用于专门化的一个尽可能直接的例子 最后一行是主要点,也是发生故障的地方。专门化的foo模板的定义决定了bar模板的专门化,或者我想要的。其他bar专门化可以在其他地方定义,或者可能不支持使用任意类型 据我所知,同样的模式也被广泛建议与enable_if一起使用 template <typename T> struct foo; template <> struct foo<int> { using type = int; }
foo
模板的定义决定了bar
模板的专门化,或者我想要的。其他bar
专门化可以在其他地方定义,或者可能不支持使用任意类型
据我所知,同样的模式也被广泛建议与enable_if
一起使用
template <typename T>
struct foo;
template <>
struct foo<int> {
using type = int;
};
template <typename T, typename use = void>
struct bar;
template <typename T>
struct bar<T, typename foo<T>::type> {
using type = typename foo<T>::type;
};
using good = typename foo<int>::type;
using bad = typename bar<int>::type;
bar
bar!=bar
您正在实例化bar
。为什么您希望编译器使用“空”定义以外的任何其他定义?您的条的“非空”专门化甚至与
参数不匹配。
$ g++ --std=c++14 special.cpp -o special
special.cpp:18:32: error: ‘type’ in ‘struct bar<int>’ does not name a type
using bad = typename bar<int>::type;
template <typename T>
struct bar<T, typename foo<T>::type> {
using type = typename foo<T>::type;
};
template <typename T>
struct bar<T, std::void_t<typename foo<T>::type>> {
using type = typename foo<T>::type;
};
template <typename T, typename AlwaysVoid = void>
struct bar;