eye*_*ash 9 c++ variadic-templates c++11
我有这个代码(从更复杂的版本简化):
template <class... A1> class Test {
public:
template <class... A2> void print (void(*function)(A2...,A1...)) {
}
};
void test_print (int a, float b, double c) {
}
int main () {
Test<float,double> test;
test.print<int> (&test_print);
}
Run Code Online (Sandbox Code Playgroud)
如果我在GCC 4.6.3上编译它使用g++ -std=c++0x filename.cpp它编译就好了但是在clang 3.0上使用clang++ -std=c++0x filename.cpp它会抛出以下错误:
filename.cpp:14:10: error: no matching member function for call to 'print'
test.print<int> (&test_print);
~~~~~^~~~~~~~~~
filename.cpp:3:33: note: candidate template ignored: failed template argument deduction
template <class... A2> void print (void(*function)(A2...,A1...)) {
^
1 error generated.
Run Code Online (Sandbox Code Playgroud)
在GCC 4.7.2上也有一个错误:
filename.cpp: In function 'int main()':
filename.cpp:14:33: error: no matching function for call to 'Test<float, double>::print(void (*)(int, float, double))'
filename.cpp:14:33: note: candidate is:
filename.cpp:3:33: note: template<class ... A2> void Test::print(void (*)(A2 ..., A1 ...)) [with A2 = {A2 ...}; A1 = {float, double}]
filename.cpp:3:33: note: template argument deduction/substitution failed:
filename.cpp:14:33: note: mismatched types 'float' and 'int'
Run Code Online (Sandbox Code Playgroud)
现在的问题是:为什么失败或我做错了什么?
作为解决http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1399的一部分,决定不可推导的参数包不会参与推导,因此保持参数列表中显式指定的参数,但不会干扰后续参数与后续函数参数的匹配。然而,这些添加的规则不一定适用于比较类型时的推导(这是根据函数调用的参数推导参数集的子算法)。
我认为该决议也应该适用于您的情况,并将其视为规范不完全明确的一个领域。您可能想向各自的编译器发送错误报告,他们可能想向 WG21 发送问题报告,要求澄清这一点(尽管我不确定我是否会说这是一个规范性规范问题 -标准在其他地方说不可推论的上下文不参与推论)。
请注意, asA1是封闭类模板的参数(因此在实例化类时成为普通参数),您可以省略它们并就模板参数推导而言保持相同的含义
class Test {
public:
template <class... A2> void print (A2..., float, double) {
}
};
int main () {
Test test;
test.print<int> (1, 1.0f, 1.0); // works
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
608 次 |
| 最近记录: |