一位朋友将模板函数缩写为clang和gcc



以下代码使用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: 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);
}

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

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错误合理吗

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

缺陷已在GCC 10.3中打开并修复。

最新更新