【发布时间】:2020-06-29 09:06:54
【问题描述】:
所以,我遇到了一段在 GCC 和 MSVC 中表现不同的代码:
#include <utility>
typedef int IType;
template<typename> struct A;
template<int... Ns>
struct A<std::integer_sequence<IType, Ns...>> {
using type = bool;
};
using B = typename A<std::make_integer_sequence<IType, 3>>::type;
int main() {
B b;
}
这很高兴在两个编译器上编译。但是,如果您将 IType 定义为 typedef long IType; MSVC 仍然有效,而 GCC 说:
source>:12:61: error: invalid use of incomplete type 'struct A<std::integer_sequence<long int, 0, 1, 2> >'
12 | using B = typename A<std::make_integer_sequence<IType, 3>>::type;
| ^~~~
<source>:5:27: note: declaration of 'struct A<std::integer_sequence<long int, 0, 1, 2> >'
5 | template<typename> struct A;
| ^
<source>: In function 'int main()':
<source>:15:3: error: 'B' was not declared in this scope
15 | B b;
| ^
Compiler returned: 1
所以,很明显,当 IType 很长时,GCC 无法使用 A 的第二个更专业的定义,因此失败了。我真的很难理解为什么 int 和 long 在这里被 GCC 区别对待。
我在 Compiler Explorer 中使用了 GCC 10.1 和 MSVC 19.24 来使用它。 https://godbolt.org/z/7L3xap
【问题讨论】: