c – Clang对GCC – 变体模板参数包后跟参数与默认值工作在GCC 4
下面的代码工作在
gcc-4.8.2中
#include <iostream> using namespace std; template<typename... Args> void func(Args... args,int optional = 0) { cout << optional << endl; } int main() { func(1); func(2.1f); // converts 2.1 to int as 'optional' parameter func<float>(3.3f); // Fine,prints '0' func(); // gcc OK,fails to compile with clang-3.5 } 并输出: $./a.out 1 2 0 0 但是如果不能用clang-3.5编译, test_variadic.cpp:15:2: error: no matching function for call to 'func' func(); ^~~~ test_variadic.cpp:5:6: note: candidate function template not viable: requires at least argument 'args',but no arguments were provided void func(Args... args,int optional = 0) ^ Clang至少警告从float到int的隐式转换.好的,我们可以通过调用func< float>这将float参数放入模板包中.所以,如果我注释掉func(),它编译很好. 我在标准语句中找不到任何东西,明确地说,可变参数模板包必须是parameter-declaration-clause中的最后一件事,只是它是一个非推断的上下文. 我的困惑来自于为什么ang声不喜欢func(),当func(1)是完全可以接受的.我可以手动定义func(int optional = 4){cout <可选的<< ENDL; }和一切都很好(但是,当传递int时,而不是模板化的函数,我正确地在clang和gcc中获得专门的func().什么是clang强制限制使用func()? 解决方法
这实际上被略微错位的
[temp.arg.explicit]/3所覆盖:
模板参数包是尾随的,因此除了func< float>(3.3f)之外的所有调用中都被推导到空包,也就是说,它们都是有效的(并且Clang将它们编译成as of 3.5). 但是,一旦我们调整模板的声明,编译器就不再符合 template <typename... Args,typename=void> void func(Args... args,int optional = 0) 现在,上述引用不适用(因为Arg不是尾随),而[temp.deduct.call]/1适用:
(即,这应该会产生扣除失败.) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |