我主要使用C++,现在使用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中工作。
但我也想知道这是为什么?