Search code examples
c++templatesgccautoc++20

A friend abbreviated template function - clang and gcc differ


The following code 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: https://stackoverflow.com/questions/62779242
    return number<decltype(a)>{a}.num + number<decltype(b)>{b}.num;
}

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

Compilation error provided by gcc (version 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;

Is it reasonable to assume this is a gcc bug?


Solution

  • Apparently it was a bug in GCC 10.1.0, after working correctly till 9.3.

    Bug was opened and fixed in GCC 10.3.