Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/161.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++ Q:带有参数包的模板专门化_C++_Templates_Specialization_Parameter Pack - Fatal编程技术网

C++ Q:带有参数包的模板专门化

C++ Q:带有参数包的模板专门化,c++,templates,specialization,parameter-pack,C++,Templates,Specialization,Parameter Pack,我马上回答这个问题 我们有模板专门化: class TestClass { public: template<typename T> static T fn(const T& a); } // OK template<typename T> T TestClass::fn(const T& a) { // ... magic for any type here ... } // OK template<> int Te

我马上回答这个问题

我们有模板专门化:


class TestClass
{
public:
   template<typename T>
   static T fn(const T& a);
}

// OK
template<typename T>
T TestClass::fn(const T& a)
{
   // ... magic for any type here ...
}


// OK
template<>
int TestClass::fn(const int& a)
{
   // ... magic for int type here ...
}


类TestClass
{
公众:
模板
静态T fn(常数T&a);
}
//嗯
模板
T测试类::fn(常量T&a)
{
//…这里任何类型的魔法。。。
}
//嗯
模板
int TestClass::fn(常量int&a)
{
//…这里的int类型的魔力。。。
}
好的。但是如果我想给函数添加一个参数包呢

class TestClass
{
public:
   template<typename T, typename... Args>
   static T fn(const T& a, Args&& ...b);
}

// OK
template<typename T, typename... Args>
T TestClass::fn(const T& a, Args&& ...b)
{
   // ... magic for any type here ...
}

// Error
template<typename... Args>
int TestClass::fn(const int& a, Args&& ...b)
{
   // ... magic for int type here ...
}

class测试类
{
公众:
模板
静态T fn(常数T&a、参数和…b);
}
//嗯
模板
T测试类::fn(常量T&a、参数&&b)
{
//…这里任何类型的魔法。。。
}
//错误
模板
int TestClass::fn(常量int&a、Args&&b)
{
//…这里的int类型的魔力。。。
}
Visual Studio给出错误E0147。 如何在不向类添加新函数的情况下执行此操作

提前感谢您的帮助

祝你今天愉快

在您的第一个示例中

template<>
int TestClass::fn(const int& a)
{ /* ... */ }
模板
int TestClass::fn(常量int&a)
{ /* ... */ }

您有一个强的>完全的专业化< /强>,这是C++模板函数/方法< /P>所允许的 但是,考虑到声明

template<typename T, typename... Args>
static T fn(const T& a, Args&& ...b);
模板
静态T fn(常数T&a、参数和…b);
你的第二个例子

template<typename... Args>
int TestClass::fn(const int& a, Args&& ...b)
{ /* ... */ }
模板
int TestClass::fn(常量int&a、Args&&b)
{ /* ... */ }
成为<强>部分特化< /强>,这是C++函数/方法不允许的。

但是您可以使用重载,避免专门化,为具有相同名称的不同模板方法添加声明

我是说

class TestClass
{
public:
   template <typename T, typename ... Args>
   static T fn (T const & a, Args && ... b);

   template <typename ... Args>
   static T fn (int const & a, Args && ... b);
};

template <typename T, typename ... Args>
T TestClass::fn (T const & a, Args && ... b)
 { /* ... */ }

template <typename ... Args>
int TestClass::fn (int const & a, Args && ... b)
 { /* ... */ }
class测试类
{
公众:
模板
静态T fn(T常数和a、参数和b);
模板
静态T fn(int const&a、Args&b);
};
模板
T TestClass::fn(T常量&a、参数&b)
{ /* ... */ }
模板
int TestClass::fn(int const&a、Args&&b)
{ /* ... */ }
在您的第一个示例中

template<>
int TestClass::fn(const int& a)
{ /* ... */ }
模板
int TestClass::fn(常量int&a)
{ /* ... */ }

您有一个强的>完全的专业化< /强>,这是C++模板函数/方法< /P>所允许的 但是,考虑到声明

template<typename T, typename... Args>
static T fn(const T& a, Args&& ...b);
模板
静态T fn(常数T&a、参数和…b);
你的第二个例子

template<typename... Args>
int TestClass::fn(const int& a, Args&& ...b)
{ /* ... */ }
模板
int TestClass::fn(常量int&a、Args&&b)
{ /* ... */ }
成为<强>部分特化< /强>,这是C++函数/方法不允许的。

但是您可以使用重载,避免专门化,为具有相同名称的不同模板方法添加声明

我是说

class TestClass
{
public:
   template <typename T, typename ... Args>
   static T fn (T const & a, Args && ... b);

   template <typename ... Args>
   static T fn (int const & a, Args && ... b);
};

template <typename T, typename ... Args>
T TestClass::fn (T const & a, Args && ... b)
 { /* ... */ }

template <typename ... Args>
int TestClass::fn (int const & a, Args && ... b)
 { /* ... */ }
class测试类
{
公众:
模板
静态T fn(T常数和a、参数和b);
模板
静态T fn(int const&a、Args&b);
};
模板
T TestClass::fn(T常量&a、参数&b)
{ /* ... */ }
模板
int TestClass::fn(int const&a、Args&&b)
{ /* ... */ }

Sad,但谢谢。Sad,但谢谢。