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++_Templates - Fatal编程技术网

C++ 使用模板将常规参数传递给函数

C++ 使用模板将常规参数传递给函数,c++,templates,C++,Templates,有一些类,它们之间的关系如下: class X:A,B class Y:A,B class Z:A,B template<template T:public A, public B> void testFunction(T generalType) { //do something } 我想使用模板将从a和B继承的常规类型传递给testFunction。我的代码如下: class X:A,B class Y:A,B class Z:A,B template<tem

有一些类,它们之间的关系如下:

class X:A,B
class Y:A,B
class Z:A,B
template<template T:public A, public B>
void testFunction(T generalType)
{
    //do something
}
我想使用模板将从a和B继承的常规类型传递给testFunction。我的代码如下:

class X:A,B
class Y:A,B
class Z:A,B
template<template T:public A, public B>
void testFunction(T generalType)
{
    //do something
}
模板
void testFunction(T generalType)
{
//做点什么
}

但我的编译器告诉我这是一个错误模板。如何修复它?

有条件地定义模板的标准方法是
std::enable\u if
。在这种情况下,您需要检查条件
std::is_base_of::value&&std::is_base_of::value

模板
的语法无效。您可以使用和
static\u assert
检查类型,如果传递了错误的类型,将触发编译器错误

template <typename T>
void testFunction(T generalType)
{
    static_assert(std::is_base_of<A, T>::value && std::is_base_of<B, T>::value, "T must inherit from A and B.");
}
模板
void testFunction(T generalType)
{

静态断言(std::is_base_of

重新打开此-假定的副本是关于类的。但与类不同,函数具有重载。这意味着您希望从重载集中排除特定函数,而不是在重载解析中选择它们,然后在稍后断开。为什么不简单地编写
模板
,看看是否有效。I d我不知道你的应用程序是如何工作的,但它可能会很好。你还有其他名为
testFunction
的函数吗?@MSalters@Aaron McDaid@songyuanyao。感谢你耐心细致的回答。在你的帮助下,我的代码通过了编译阶段。我从你们身上学到了很多东西,