朋友简写的模板函数——clang和gcc的区别

A friend abbreviated template function - clang and gcc differ

以下代码compiles with clang but not with 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: 
    return number<decltype(a)>{a}.num + number<decltype(b)>{b}.num;
}

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

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;

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

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

Bug was opened and fixed in GCC 10.3.