类模板参数推导-clang和gcc不同 [英] Class Template Argument Deduction - clang and gcc differ

查看:73
本文介绍了类模板参数推导-clang和gcc不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码使用gcc而不是clang编译:

template<typename T>
class number {
    T num;
public:
    number(T num = 0): num(num) {}
    
    template<typename T1, typename T2>
    friend auto add(T1 a, T2 b);
};

template<typename T1, typename T2>
auto add(T1 a, T2 b) {
    // this compiles with both:
        // return number<T1>{a}.num + number<T2>{b}.num;
    // this compiles only with gcc:
    return number{a}.num + number{b}.num; // <== clang is unhappy here
}

int main() {
    auto result = add(1.0, 2.0);
}

clang提供的编译错误(带有-std = c ++ 20的10.0.0版):

Compilation errors provided by clang (version 10.0.0 with -std=c++20):

error: member reference base type 'number' is not a structure or union

    return number{a}.num + number{b}.num;

           ~~~~~~~~~^~~~

error: member reference base type 'number' is not a structure or union

    return number{a}.num + number{b}.num;

                           ~~~~~~~~~^~~~

正确的行为是什么?

推荐答案

正如Barry在评论中所提到的,这是一个叮叮当当的错误 47870 ).

As mentioned by Barry in the comments, this was a clang bug 44468 (and 47870).

已修复错误.现在,代码可以在Clang干线中正常工作.

Bug fixed. Code now compiles and works well in Clang trunk.

这篇关于类模板参数推导-clang和gcc不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆