Class 模板参数推导 - clang 和 gcc 不同
Class Template Argument Deduction - clang and gcc differ
以下代码compiles with gcc but not with clang:
template<typename T>
class number {
T num;
public:
number(T num = 0): num(num) {}
template<typename T1, typename T2>
friend auto add(T1 a, T2 b);
};
template<typename T1, typename T2>
auto add(T1 a, T2 b) {
// this compiles with both:
// return number<T1>{a}.num + number<T2>{b}.num;
// this compiles only with gcc:
return number{a}.num + number{b}.num; // <== clang is unhappy here
}
int main() {
auto result = add(1.0, 2.0);
}
clang 提供的编译错误(版本 10.0.0 with -std=c++20):
error: member reference base type 'number' is not a structure or union
return number{a}.num + number{b}.num;
~~~~~~~~~^~~~
error: member reference base type 'number' is not a structure or union
return number{a}.num + number{b}.num;
~~~~~~~~~^~~~
正确的行为是什么?
正如 Barry 在评论中提到的,这是一个 clang 错误 44468 (and 47870)。
错误已修复。代码现在可以编译并运行良好 in Clang trunk.
以下代码compiles with gcc but not with clang:
template<typename T>
class number {
T num;
public:
number(T num = 0): num(num) {}
template<typename T1, typename T2>
friend auto add(T1 a, T2 b);
};
template<typename T1, typename T2>
auto add(T1 a, T2 b) {
// this compiles with both:
// return number<T1>{a}.num + number<T2>{b}.num;
// this compiles only with gcc:
return number{a}.num + number{b}.num; // <== clang is unhappy here
}
int main() {
auto result = add(1.0, 2.0);
}
clang 提供的编译错误(版本 10.0.0 with -std=c++20):
error: member reference base type 'number' is not a structure or union
return number{a}.num + number{b}.num;
~~~~~~~~~^~~~
error: member reference base type 'number' is not a structure or union
return number{a}.num + number{b}.num;
~~~~~~~~~^~~~
正确的行为是什么?
正如 Barry 在评论中提到的,这是一个 clang 错误 44468 (and 47870)。
错误已修复。代码现在可以编译并运行良好 in Clang trunk.