Class Template Argument Deduction - clang and gcc differ

与世无争的帅哥 提交于 2020-07-20 17:04:12

问题


The following code compiles with gcc but not with 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);
}

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;

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

What is the correct behavior?

来源:https://stackoverflow.com/questions/62779242/class-template-argument-deduction-clang-and-gcc-differ

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!