Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/145.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++ C++;,模板化模板规范_C++_Templates_Specifications - Fatal编程技术网

C++ C++;,模板化模板规范

C++ C++;,模板化模板规范,c++,templates,specifications,C++,Templates,Specifications,我有一个模板,它没有实现两种方法 我只想使用作为模板规范的类,对于那些我提供规范的类 例如: template<class T> class Temp { void Method1(); }; Temp<int>::method1() {...} Temp模板类 { void方法1(); }; Temp::method1(){…} 现在我想为规范提供一个模板类型,比如 template<class General> void Temp<Gene

我有一个模板,它没有实现两种方法

我只想使用作为模板规范的类,对于那些我提供规范的类

例如:

template<class T> class Temp
{
  void Method1();
};

Temp<int>::method1() {...}
Temp模板类
{
void方法1();
};
Temp::method1(){…}
现在我想为规范提供一个模板类型,比如

template<class General> void Temp<General> method1() {...}
template void Temp method1(){…}
我将指定哪些类型是
General


<如何> C++语法?

看起来你想为已知类型的列表提供一系列专门化。为了实现这一点,您可以使用SFINAE(例如,
std::enable_if
)和一些模板元编程

给你一个想法:

#include <type_traits>

template <class T, class... Candidates>
struct is_one_of;

/* alternative 1 - too complicated
template <class T, class Head, class... Tail>
struct is_one_of<T, Head, Tail...> 
  : std::integral_constant<bool, std::is_same<T, Head>::value || is_one_of<T, Tail...>::value>
{}; */

/* better alternative - thanks, Dan */
template <class T, class Head, class... Tail>
struct is_one_of<T, Head, Tail...> : is_one_of<T, Tail...> {}

template <class T, class... Tail>
struct is_one_of<T, T, Tail...> : std::true_type {}

/* needed for both alternatives */
template <class T>
struct is_one_of<T> : std::false_type {};
#包括
模板
结构是其中之一;
/*备选方案1-过于复杂
模板
结构是其中之一
:std::积分常数
{}; */
/*更好的选择-谢谢你,丹*/
模板
结构是{的}的{之一}
模板
struct是:std::true_类型{}中的一个
/*两种方案都需要*/
模板
结构是:std::false类型{}中的一个;

现在
enable\u如果
你的专业化取决于
是移动btw发送的

之一…对不起,如果我有一些打字错误,模板结构是{之一};模板结构是以下类型之一:std::true\u type{};