C++ Visual Studio初始化隐式静态成员失败

C++ Visual Studio初始化隐式静态成员失败,c++,visual-c++,initialization,c++14,static-members,C++,Visual C++,Initialization,C++14,Static Members,以下面的片段为例 #include <iostream> using namespace std; template<int i, typename T = int> struct A { T num = i; A<i, T>() { cout << "Instantiated a A<" << i << ">" << endl; } }; temp

以下面的片段为例

#include <iostream>
using namespace std;

template<int i, typename T = int> struct A
{
    T num = i;
    A<i, T>()
    {
        cout << "Instantiated a A<" << i << ">" << endl;
    }
};

template<int i, int i2> struct B
{
    static A<i> a;
    static A<i * i2> a2;
};
template<int i, int i2> A<i> B<i, i2>::a{};
template<int i, int i2> A<i * i2> B<i, i2>::a2{};

template<typename T> struct C
{
    static void doSomething()
    {
        cout << "Have a A<" << T::a.num << "> and a A<" << T::a2.num << "> in C" << endl;
    }
};

int main() {
    typedef C<B<2, 2>> c;
    cout << "Typedefined a C\nCalling static member function to initialize C<B<2, 2>>'s B<2, 2>'s A<>s" << endl;
    c::doSomething();
    return 0;
}
然后是一些注释

main.cpp(19): note: while compiling class template static data member 'A<2,int> B<2,2>::a'
main.cpp(26): note: see reference to class template instantiation 'B<2,2>' being compiled
main.cpp(25): note: while compiling class template member function 'void C<B<2,2>>::doSomething(void)'
main.cpp(33): note: see reference to function template instantiation 'void C<B<2,2>>::doSomething(void)' being compiled

main.cpp(33): note: see reference to class template instantiation 'C<B<2,2>>' being compiled
main.cpp(19):注意:编译类模板静态数据成员'ab::A'
cpp(26):注意:请参阅对正在编译的类模板实例化“B”的引用
cpp(25):注意:在编译类模板成员函数“void C::doSomething(void)”时
main.cpp(33):注意:请参阅正在编译的函数模板实例化“void C::doSomething(void)”的参考
cpp(33):注意:请参阅对正在编译的类模板实例化“C”的引用

>这里发生了什么?< /P> < P>我能够减少测试用例,识别问题(这似乎是VisualC++编译器中的一个bug),并找到一个解决方案:

#include <iostream>
using namespace std;

template<int i> struct A
{
    int num = i;
    A() {}
};

template<int i> struct B
{
    static A<i> a;
};

// MSVC doesn't like this syntax
//                         |||
//                         vvv
template<int i> A<i> B<i>::a{};

// To fix the error, rewrite the above line in one of the below ways:
//
// template<int i> A<i> B<i>::a;
// template<int i> A<i> B<i>::a = {};

int main() {
    typedef B<2> B2;
    cout << B2::a.num << endl;
    return 0;
}
#包括
使用名称空间std;
模板结构A
{
int num=i;
A(){}
};
模板结构B
{
静态A;
};
//MSVC不喜欢这种语法
//                         |||
//vvv
模板ab::A{};
//要修复错误,请使用以下方法之一重写上述行:
//
//模板A、B::A;
//模板A B::A={};
int main(){
类型定义B B2;

CUT< P>我能够减少测试用例,识别问题(这似乎是VisualC++编译器中的一个bug),并找到一个解决方案:

#include <iostream>
using namespace std;

template<int i> struct A
{
    int num = i;
    A() {}
};

template<int i> struct B
{
    static A<i> a;
};

// MSVC doesn't like this syntax
//                         |||
//                         vvv
template<int i> A<i> B<i>::a{};

// To fix the error, rewrite the above line in one of the below ways:
//
// template<int i> A<i> B<i>::a;
// template<int i> A<i> B<i>::a = {};

int main() {
    typedef B<2> B2;
    cout << B2::a.num << endl;
    return 0;
}
#包括
使用名称空间std;
模板结构A
{
int num=i;
A(){}
};
模板结构B
{
静态A;
};
//MSVC不喜欢这种语法
//                         |||
//vvv
模板ab::A{};
//要修复错误,请使用以下方法之一重写上述行:
//
//模板A、B::A;
//模板A B::A={};
int main(){
类型定义B B2;

可以这么说,这是用clang 3.8编译的,std=c++14也没有问题。但是,我很困惑,当你发布的整个代码在
main()
@WhozCraig中的结尾
}中只有34行时,为什么
main.cpp
第37行会报告错误(一个include和
\u getch()
)。奇怪的是,它在文件结束后报告了一个错误…
A()
这个语法在做什么?@OutlawLemur:是的,但是
有什么用呢?构造函数
A
是模板类
A
的成员;它不需要有模板参数就可以访问它们。我很惊讶它可以编译。@Nicolas我们可以使用
A()
因为注入了类名,每个人都会这样做,所以虽然
A()
看起来非常奇怪,但这并没有错(除了我认为我从未见过有人这样做,这让它看起来非常尴尬).Fwiw,这是用clang 3.8编译的,std=c++14也没有问题。但是,我很困惑
main.cpp
第37行怎么会报告一个错误,当您发布用于复制的整个代码只有34行通过
main()
@WhozCraig中的结束
}
时,可视化实现还有2行(一个include和
\u getch()
)。奇怪的是,它在文件结束后报告了一个错误…
A()
这个语法在做什么?@OutlawLemur:是的,但是
有什么用呢?构造函数
A
是模板类
A
的成员;它不需要有模板参数就可以访问它们。我很惊讶它可以编译。@Nicolas我们可以使用
A()
因为注入了类名,每个人都会这样做,所以虽然
A()
看起来非常奇怪,但这并没有错(除了我认为我从未见过有人这样做,这让它看起来非常尴尬).噢,微软…谢谢你向微软报告这个错误吗?如果是,你能在这里发布一个错误报告的链接吗?@ildjarn不,我没有。噢,微软…谢谢你向微软报告这个错误吗?如果是,你能在这里发布一个错误报告的链接吗?@ildjarn不,我没有。