c – Clang对GCC – 变体模板参数包后跟参数与默认值工作在GCC 4.8,但不是Clang 3.5

c – Clang对GCC – 变体模板参数包后跟参数与默认值工作在GCC 4.8,但不是Clang 3.5,第1张

概述下面的代码工作在 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 下面的代码工作在 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.out1200

但是如果不能用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 provIDedvoID 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所覆盖:

A trailing template parameter pack (14.5.3) not
otherwise deduced will be deduced to an empty sequence of template arguments.

模板参数包是尾随的,因此除了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适用:

When a function parameter pack appears in a non-deduced context
(14.8.2.5),the type of that parameter pack is never deduced.

(即,这应该会产生扣除失败.)

总结

以上是内存溢出为你收集整理的c – Clang对GCC – 变体模板参数包后跟参数与默认值工作在GCC 4.8,但不是Clang 3.5全部内容,希望文章能够帮你解决c – Clang对GCC – 变体模板参数包后跟参数与默认值工作在GCC 4.8,但不是Clang 3.5所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1246533.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存