考虑以下代码:
template <typename T> using VoidT = void;
class A {
public:
using TEST = int;
};
class C {
public:
using DIFFERENT = int;
};
template <typename T, typename Enable = void>
class B {
public:
B() = delete;
};
template <typename T>
class B<T, VoidT<typename T::TEST>> {
public:
B() = default;
};
template <typename T>
class B<T, VoidT<typename T::DIFFERENT>> {
public:
B() = default;
};
int main() {
B<A> a;
B<C> b;
return 0;
}
使用g ++-4.8.5,编译此代码会给我以下错误消息:
~/test/compile_test> g++ -std=c++11 test.cpp
test.cpp:31:7: error: redefinition of ‘class B<T, void>’
test.cpp:24:7: error: previous definition of ‘class B<T, void>’
但是,当我使用g ++-8.3(例如ideone)进行编译时,代码将被编译,并且正确处理了不同的专业领域。这是在GCC中修复的错误,还是我以某种方式调用未定义的行为(因此,编译器行为的差异是有争议的-它是未定义的)?