难道是GCC巨大的BUG,模板类的静态成员无法调用构造函数?
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
而静态变量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
...全文
相关推荐
list.h 用模板类封装实现了单链表的头文件,可直接调用(GCC)
C++数据结构【链表】运用模板类把单链表做成了一个list.h头文件。学C++数据结构与算法的都来看一看吧!对你们应该会有帮助!可以了解一下单链表是如何实现的,包括一些基本的使用函数。
【Bug已解决】gcc: internal compiler error: Segmentation fault 解决方案
在编译某个 C/C++/CUDA 扩展(常见于从源码构建 vLLM、flash-attn、xformers 等)时,gcc在编译某个源文件的过程中突然崩溃,报内部编译器错误(ICE)。
关于安装linux redhat后无法使用yum命令安装gcc-c++问题的解决过程
"在Linux Redhat系统中遇到无法使用yum命令安装gcc的问题,解决方法包括安装Centos镜像,修改repo文件以及清除缓存。"在Linux系统中,特别是对于Redhat系列的操作系
调用GCC编译器验证代码正确性
在C#中,我们可以利用进程类(`System.Diagnostics.Process`)来调用GCC编译器并捕获其输出。
Linux静态链接库与模板类的处理方式
本文主要探讨了Linux系统中如何处理模板类与静态链接库的结合问题。首先,让我们回顾一下在创建静态链接库的一般步骤:1. **模板类的封装**:模板类`SORT`被定义在`lib_test.h`头


