本文介绍了如何专门化c ++可变参数模板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我正在尝试理解c ++可变参数模板。 我不太清楚使用正确的语言来解释我想要实现的内容,所以如果我可能会更简单提供一些代码来说明我想要实现的目标。 I'm trying to understand c++ variadic templates.I'm not much aware of the correct language to use to explain what I'd like to achieve, so it might be simpler if I provide a bit of code which illustrate what I'd like to achieve.#include <iostream>#include <string>using namespace std;template<int ...A>class TestTemplate1 {public: string getString() { return "Normal"; }};template<int T, int ...A>string TestTemplate1<2, A...>::getString() { return "Specialized";}template<typename ...A>class TestTemplate2 {};int main() { TestTemplate1<1, 2, 3, 4> t1_1; TestTemplate1<1, 2> t1_2; TestTemplate1<> t1_3; TestTemplate1<2> t1_4; TestTemplate2<> t2_1; TestTemplate2<int, double> t2_2; cout << t1_1.getString() << endl; cout << t1_2.getString() << endl; cout << t1_3.getString() << endl; cout << t1_4.getString() << endl;} 这会引发一些错误。 - 错误C2333:'TestTemplate1<> :: getString':函数声明中的错误;跳过函数体 - 错误C2333:'TestTemplate1< 1,2,3,4> :: getString':函数声明中的错误;跳过函数体 - 错误C2333:'TestTemplate1< 1,2> :: getString':函数声明中的错误;跳过函数体 - 错误C2333:'TestTemplate1< 2> :: getString':函数声明中的错误;跳过函数体 - 错误C2977:'TestTemplate1< A ...>':模板参数太多 - 错误C2995:'std :: string TestTemplate1< A ...> :: getString(void)':函数模板已经定义 - 错误C3860:类模板名称后面的模板参数列表必须按照模板参数列表中使用的顺序列出参数 我怎样才能为每个`TestTemplate1< 2,...>`实例提供专门的行为,比如t1_4?This throws several errors. - error C2333: 'TestTemplate1<>::getString' : error in function declaration; skipping function body - error C2333: 'TestTemplate1<1,2,3,4>::getString' : error in function declaration; skipping function body - error C2333: 'TestTemplate1<1,2>::getString' : error in function declaration; skipping function body - error C2333: 'TestTemplate1<2>::getString' : error in function declaration; skipping function body - error C2977: 'TestTemplate1<A...>' : too many template arguments - error C2995: 'std::string TestTemplate1<A...>::getString(void)' : function template has already been defined - error C3860: template argument list following class template name must list parameters in the order used in template parameter listHow can I have a specialized behavior for every `TestTemplate1<2, ...>` instances like t1_4?推荐答案 第二个定义(几乎)与第一个定义相同。它根本不是专业化。 模板专业化要求你实例化所有参数。 E. g。 The second definition is (almost) the same as the first. It is not a specialization at all.A template specialization requires that you instantiate all the arguments. E. g. template <2, 3, 4>string TestTemplate1() {} 您也可以尝试部分特化,但我不确定语法并且没有可用于测试的编译器 - 应该是......像这样: You could also try a partial specialization, but I'm not sure about the syntax and have no compiler available to test that - it should be sth. like this:template <2, int ...a>string TestTemplate1(){} 这就是说,我发现编译器在标准出现时落后于标准模板功能。对类的支持要好得多,语法通常也更清晰。因此,另一种选择是定义一个简单的模板化结构或类,并将你的函数声明为非模板化的公共方法。That said, I've found compilers to be lagging behind the standard when it comes to template functions. Support for classes is considerably better, and the syntax is also often clearer. therefore an alternate option would be to define a simple templated struct or class, and your declare your function as a non-templated public method. 感谢Stefan_Lang,你让我看到了新的方向。 我实际上不得不专攻课程(不仅仅是功能)。 这是: Thanks Stefan_Lang, you got me looking in new directions.I actually had to specialize the class (not just the function).Here it is:#include <iostream>#include <string>using namespace std;template<int ...A>class TestTemplate1 {public: template<int ...A> string getString() { return "Normal"; }};template<int ...A>class TestTemplate1<2, A...> { // Define a class specializationpublic: string TestTemplate1<2, A...>::getString() { return "Specialized"; }};template<typename ...A>class TestTemplate2 {};int main() { TestTemplate1<1, 2, 3, 4> t1_1; TestTemplate1<1, 2> t1_2; TestTemplate1<> t1_3; TestTemplate1<2> t1_4; TestTemplate2<> t2_1; TestTemplate2<int, double> t2_2; cout << t1_1.getString() << endl; cout << t1_2.getString() << endl; cout << t1_3.getString() << endl; cout << t1_4.getString() << endl;} 这篇关于如何专门化c ++可变参数模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-16 08:45