简体   繁体   中英

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 ?

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM