一个朋友的缩写模板函数——clang和gcc的区别

Ami*_*rsh 5 c++ gcc templates auto c++20

以下代码使用 clang 编译,但不使用 gcc 编译

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

auto add(auto a, auto b) {
    // the decltype(a) is needed to make clang happy
    // see: /sf/ask/4394546971/
    return number<decltype(a)>{a}.num + number<decltype(b)>{b}.num;
}

int main() {
    auto result = add(1.0, 2.0);
}
Run Code Online (Sandbox Code Playgroud)

gcc 提供的编译错误(版本 10.1 with -std=c++20):

In instantiation of 'auto add(auto:13, auto:14) [with auto:13 = double; auto:14 = double]':
error: 'double number<double>::num' is private within this context
   return number<decltype(a)>{a}.num + number<decltype(b)>{b}.num;
Run Code Online (Sandbox Code Playgroud)

假设这是一个gcc 错误是否合理?

Ami*_*rsh 0

显然这是 GCC 10.1.0 中的一个错误,在 9.3 之前都可以正常工作。

Bug 在 GCC 10.3 中被打开并修复