以下代码无法在 VS2015 中编译。
struct Foo
{
Foo(int value) { }
};
struct Moo
{
struct
{
Foo foo = 0;
} fooHolder;
};
int main()
{
Moo moo;
}
显示以下错误。
1>c:\xxx\main.cpp(81): error C2512: 'Foo' : no appropriate default constructor available
1> This diagnostic occurred in the compiler generated function 'Moo::<unnamed-type-fooHolder>::(void) restrict(cpu, amp)'
如果未命名的结构被赋予了名称,代码就会编译。
struct NamedHolder
{
Foo foo = 0;
} fooHolder;
用clang和gcc编译的代码。 http://coliru.stacked-crooked.com/a/3b4ab035a967eed9
是否拒绝有效代码?
最佳答案
此代码非常好,它使用 VS2015 Update 1 RC(刚刚验证)编译。也许你错过了什么。我测试的系统:
Microsoft Visual Studio Community 2015
Version 14.0.24627.00 Update 1 RC
Microsoft .NET Framework
Version 4.6.01040
Installed Version: Community
Visual C++ 2015 RC 00322-20000-00000-AA392 Microsoft Visual C++ 2015 RC
...
关于c++ - VS2015拒绝未命名类型的类内初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33845459/