难道是GCC巨大的BUG,模板类的静态成员无法调用构造函数?

conanfans conanfans 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
A为模板类,其中有一个静态成员s类型为B。GCC编译、运行都成功,但是无法调用B的构造函数。
而静态变量s1却可以正常的初始化。
为什么对象的构造函数不能执行呢?

#include <iostream>
#include "h.h"
int main()
{
A<string> objA(111);

}

h.h:
****************************************************
#ifndef POOL_H_INCLUDED_GF
#define POOL_H_INCLUDED_GF
#include <iostream>
#include "s.h"
using namespace std;
template <typename T>
class A
{
public:
int a1;
A(int a1)
{
this->a1=a1;
cout<<"init A"<<endl;
cout<<"sizeof T ="<<sizeof(T)<<endl;
cout<<"s1="<<A::s1<<endl;
}

static int s1;
static B s;
};

template <typename T> int A<T>::s1=1234;
template <typename T> B A<T>::s;

#endif


s.h
*****************************************************
#ifndef SH
#define SH
#include <iostream>
using namespace std;
class B
{
public:
int m1;
B()
{
cout<<"init static 0"<<endl;
}
};
#endif
...全文

相关推荐

评论 7
收藏 已收藏
分享
返回顶部
conanfans
C++ 语言 65,214 成员 250,514 内容