加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – Clang对GCC – 变体模板参数包后跟参数与默认值工作在GCC 4

发布时间:2020-12-16 05:21:43 所属栏目:百科 来源:网络整理
导读:下面的代码工作在 gcc-4.8.2中 #include iostreamusing namespace std;templatetypename... Argsvoid func(Args... args,int optional = 0){ cout optional endl;}int main(){ func(1); func(2.1f); // converts 2.1 to int as 'optional' parameter funcflo
下面的代码工作在 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所覆盖:

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.

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读