[英]Different compiler behaviour when using alias as scope to get parent member
This code compiles fine on Clang and Visual C++ but not on GCC:此代码在 Clang 和 Visual C++ 上编译良好,但在 GCC 上编译良好:
#include <iostream>
template <class T>
struct Test {
Test(T &t) : _t(t) {
}
void method() {
std::cout << _t.Internal::_value << "\n"; // Doesn't work on GCC
std::cout << _t.T::Internal::_value << "\n"; // Work on all compilers
}
private:
T &_t;
};
template <class T>
struct Base {
T _value = 1;
};
template <class T>
struct Child : Base<int> {
using Internal = Base<int>;
int _value = 2;
};
int main(int argc, const char * argv[]) {
Child<float> child;
Test<Child<float>> test(child);
test.method();
return 0;
}
The error message from GCC is来自 GCC 的错误消息是
error: 'Internal' has not been declared
9 | std::cout << _t.Internal::_value << "\n";
| ^~~~~~~~
Which one is right?哪一个是对的?
Visual C++ and Clang are right in accepting this code. Visual C++ 和 Clang 正确接受此代码。
It was a bug in GCC that prevented it from doing the same.这是 GCC 中的一个错误,它阻止了它做同样的事情。 The error as in the question was up to GCC 10, in GCC 11 its wording changed to
问题中的错误高达 GCC 10,在 GCC 11 中,其措辞更改为
error: request for member 'Internal' in non-class type 'T'
9 | std::cout << _t.Internal::_value << "\n";
| ^~~~~~~~
And GCC trunk finally accepts the code as well.并且 GCC 中继最终也接受了代码。 Demo: https://gcc.godbolt.org/z/dj34Yhns3
演示: https://gcc.godbolt.org/z/dj34Yhns3
So we could expect the fix in GCC 12.所以我们可以期待 GCC 12 中的修复。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.