Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/156.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 模板:未应用SFINAE专门化_C++_Templates_Sfinae_Specialization - Fatal编程技术网

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;