简体   繁体   English

Class 模板参数推导 - clang 和 gcc 不同

[英]Class Template Argument Deduction - clang and gcc differ

The following code compiles with gcc but not with clang :以下代码使用 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);
}

Compilation errors provided by clang (version 10.0.0 with -std=c++20): clang(版本 10.0.0 与 -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?什么是正确的行为?

As mentioned by Barry in the comments, this was a clang bug 44468 (and 47870 ).正如 Barry 在评论中提到的,这是一个 clang 错误44468 (和47870 )。

Bug fixed.错误已修复。 Code now compiles and works well in Clang trunk .代码现在可以在 Clang trunk 中编译并运行良好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 匿名临时和类模板参数演绎 - gcc vs clang - Anonymous temporaries and class template argument deduction - gcc vs clang 函数指针数组的类模板参数推论适用于clang,但不适用于gcc - Class template argument deduction for array of function pointer works on clang, but not on gcc 带括号的“嵌套”类模板参数推导:GCC 与 clang - "Nested" class-template argument deduction with parentheses: GCC vs. clang 为什么这个模板参数推导在 GCC 而不是 Clang 上失败? - Why does this template argument deduction fail on GCC but not Clang? Function 模板参数推导在 gcc 中有效,但在 msvc 和 clang 中无效 - Function template argument deduction works in gcc but not in msvc and clang Clang 6 中临时对象的模板参数推导中断 - Template Argument Deduction Broken in Clang 6 for Temporary Objects 朋友缩写模板 function - clang 和 gcc 不同 - A friend abbreviated template function - clang and gcc differ GCC模板参数推导/替换失败 - GCC template argument deduction/substitution failed 类中的函数模板参数推导 - function template argument deduction in a class 变量模板作为模板参数:演绎适用于GCC但不适用于Clang - Variadic template as template parameter: deduction works with GCC but not with Clang
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM