我主要使用 G++,现在使用 Visual Studio 2015。我想用 VC++2015 构建我的项目,但我收到错误消息,指出在给定默认参数的函数中无效使用 '::' 并带有前向声明的强类型枚举。
这是一些代码:
struct Foo
{
//! Forward declaration of Bar
enum class Bar : short;
//! "Faulty" function with default argument
void DoSmth(Bar aBar = Bar::Baz)
{
// ... code ...
}
//! Complete declaration of Bar
enum class Bar : short
{
Baz
};
};
int main() { }
在使用默认参数 Bar::Baz 声明函数 DoSmth() 时,它给了我以下错误:
test.cpp(7): error C2589: '::': illegal token on right side of '::'
test.cpp(7): error C2059: syntax error: '::'
test.cpp(17): fatal error C1903: unable to recover from previous error(s); stopping compilation
使用 G++(使用 4.9 和 5.1 测试)代码编译得很好,但使用 VC++2015 则不行。
我完全知道我必须在使用前声明一些东西,但是。仅仅是因为 VC++2015 没有在类的范围内寻找 Bar 的完整声明和定义,而 G++ 却可以吗?或者,也许 G++ 只是采用完整的声明并将其与前向声明“合并”(因为它们在同一范围内),从而使其对类完全可用?或者也许我完全错了,完全不同的东西导致了这个?
我可以忍受我必须更改所有强类型枚举的声明才能使其与 VC++2015 一起使用。
但我也想知道这是为什么?