禁止从模板类的库标头中"Base class ‘class X’ should be explicitly initialized in the copy constructor"
Suppressing "Base class ‘class X’ should be explicitly initialized in the copy constructor" from library header for template class
我遇到了一个类似的问题。
我正在使用第三方图书馆。它在文件headers/things.h
:中定义类如下(由于许可问题,伪造了所有名称)
class A {
public:
virtual ~A() {}
};
template < class T, class U >
class B : virtual public A {
public:
B(T) {}
B(const B< T,U >) {}
virtual ~B() {}
};
template < class T, class U >
class C : virtual public B< T, U > {
public:
C(T t) : B < T, U > (t) {}
C(const C< T,U > &other) : B< T,U >(other) {}
~C() {}
};
另外,仍在库头中:
typedef C< int, int > CC;
错误消息为:
cc1plus: warnings being treated as errors
../headers/things.h: In copy constructor ‘C<T, U>::C(const C<T, U>&) [with T = int, U = int]’:
things.cpp:5: instantiated from here
../headers/things.h:22: warning: base class ‘class A’ should be explicitly initialized in the copy constructor
../headers/things.h: In copy constructor ‘B<T, U>::B(const B<T, U>&) [with T = int, U = int]’:
../headers/things.h:23: instantiated from ‘C<T, U>::C(const C<T, U>&) [with T = int, U = int]’
things.cpp:5: instantiated from here
../headers/things.h:12: warning: base class ‘class A’ should be explicitly initialized in the copy constructor
在things.cpp
中,我有:
#include "things.h"
CC make_cc() {
CC cc(123);
return cc;
}
int main() {
CC cc = make_cc();
return 0;
}
文件布局为:
../
|-- source
| `-- things.cpp
`-- headers
`-- things.h
我知道这个警告意味着什么,我并没有要求这样做。由于它在第三方库中,出于维护原因,我非常不愿意修改(修复)它。我只是想忽略这个警告。
我正在用编译我的代码
g++ things.cpp -o things -Wall -Wextra -Werror -isystem ../headers/
我使用-isystem
来指定目录,因为gcc文档声明:
GCC处理系统标头时,除"#警告"(请参阅诊断)生成的警告外,所有警告都将被抑制。(…)-isystem命令行选项将其参数添加到目录列表中以搜索标头,就像-I一样。在该目录中找到的任何标头都将被视为系统标头。
这似乎总的来说是有效的,因为几乎所有来自第三方图书馆的警告都被压制了。
不幸的是,由于此声明恰好是typedef
模板化类的实例化,编译器认为它正在编译的是my代码,而不是(伪)系统头。
正如在引用的问题中所说,不可能只抑制这个警告,我必须禁用-Wextra
,这是我不想做的
问题:是否可以抑制此警告?让gcc知道这不是我的代码,而是库代码?
我使用的是gcc 4.1.2。
正如其他人所提到的,使用较新的编译器似乎是比以下方法更好的方法,但由于您的手似乎被束缚住了,请考虑以下方法:
沿着的路线围绕第三方库类创建包装器类
class MyCC {
CC cc;
};
template<typename T, typename U>
class MyB {
B<T, U> b;
};
转发任何相关的函数调用,使包装器尽可能透明。或者,您可以使用类似BOOstrongTRONG_TYPEDEF的东西。
定义这些包装器的头可以位于文件的顶部:
#pragma GCC system_header
希望pragma能在直接使用库的仅的客户端中抑制警告,而所有其他代码都可以使用包装器,因此不需要pragma。
- 这个语法std::class<>{}(arg1, arg2) 在C++中是什么意思?
- C++核心准则 C35 对于接口类"A base class destructor should be either public and virtual, or protected and nonv
- 使用模板进行堆栈实现; "name followed by :: must be a class or namespace"
- "Undefined class"作为is_base_of的论据
- 如何将class.cpp和class.hpp编译为一个.o文件
- '{'标记之前的预期类名,然后在预声明时无效使用不完整的类型'class class_name'
- 如何使用"derived input class"创建派生类?
- 错误:"Left of getValue must have class/struct/union"
- ' class a : b ' 和 ' class a : public b ' 之间的继承类不同
- Pybind11 Class Definition
- 在C++中使用没有合作伙伴Class/Cpp文件的头文件是否实用
- C2011 'CMemDC':Visual Studio 2019中的'class'类型重新定义
- TypeError: [c++ addon class] 不是构造函数
- C++ class template
- 当初始值设定项是基类名时'initializer does not name a non-static data member or base class'错误
- Clearing Class Foo with new(pFoo) Foo()
- C++ class vs a library
- 继承类时"invalid use of incomplete type ‘class tree_node_t’"
- 使用"class"关键字,后跟未声明的标识符
- 禁止从模板类的库标头中"Base class ‘class X’ should be explicitly initialized in the copy constructor"