G++ 不允许我定义名为 "major" 的成员函数
G++ doesn't allow me to define a member function named "major"
所以,今天我正在编写一些单元测试,突然 G++ 给了我一个关于 GNU C 和我的一个名为major
的成员函数的意外警告。为什么在不触发 G++ 的情况下不能有一个名为major
的成员函数?
这是一个最小可行的测试代码段:
// Any of these includes trigger the warnings
#include <random>
#include <cstdlib>
class my_class {
public:
void major() const;
};
inline void my_class::major() const {}
int main(void) {
my_class my_obj;
my_obj.major();
return 0;
}
这是编译的输出(使用g++ --std=c++14 -o test-gcc-major test-gcc-major.cpp
):
[flisboac@sonic ~]$ uname -a && lsb_release -a && g++ -v && g++ --std=c++14 -o test-gcc-major test-gcc-major.cpp && ./test-gcc-major
Linux sonic 4.12.10-1-ARCH #1 SMP PREEMPT Wed Aug 30 12:18:42 CEST 2017 x86_64 GNU/Linux
LSB Version: 1.4
Distributor ID: Arch
Description: Arch Linux
Release: rolling
Codename: n/a
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-pc-linux-gnu/7.1.1/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: /build/gcc-multilib/src/gcc/configure --prefix=/usr --libdir=/usr/lib --libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=https://bugs.archlinux.org/ --enable-languages=c,c++,ada,fortran,go,lto,objc,obj-c++ --enable-shared --enable-threads=posix --enable-libmpx --with-system-zlib --with-isl --enable-__cxa_atexit --disable-libunwind-exceptions --enable-clocale=gnu --disable-libstdcxx-pch --disable-libssp --enable-gnu-unique-object --enable-linker-build-id --enable-lto --enable-plugin --enable-install-libiberty --with-linker-hash-style=gnu --enable-gnu-indirect-function --enable-multilib --disable-werror --enable-checking=release --enable-default-pie --enable-default-ssp
Thread model: posix
gcc version 7.1.1 20170630 (GCC)
test-gcc-major.cpp:7:13: warning: In the GNU C Library, "major" is defined
by <sys/sysmacros.h>. For historical compatibility, it is
currently defined by <sys/types.h> as well, but we plan to
remove this soon. To use "major", include <sys/sysmacros.h>
directly. If you did not intend to use a system-defined macro
"major", you should undefine it after including <sys/types.h>.
void major() const;
^~~~~~~~
test-gcc-major.cpp:10:13: warning: In the GNU C Library, "major" is defined
by <sys/sysmacros.h>. For historical compatibility, it is
currently defined by <sys/types.h> as well, but we plan to
remove this soon. To use "major", include <sys/sysmacros.h>
directly. If you did not intend to use a system-defined macro
"major", you should undefine it after including <sys/types.h>.
inline void my_class::major() const {}
^~~~~~~~~~~~~~~~~~~~~~~~~~
test-gcc-major.cpp:14:13: warning: In the GNU C Library, "major" is defined
by <sys/sysmacros.h>. For historical compatibility, it is
currently defined by <sys/types.h> as well, but we plan to
remove this soon. To use "major", include <sys/sysmacros.h>
directly. If you did not intend to use a system-defined macro
"major", you should undefine it after including <sys/types.h>.
my_obj.major();
对于以任何方式引用成员函数的每一行,都会触发警告。此外,我无法取消定义任何东西,因为我正在实现一个库,而这个负担应该落在最终用户身上。
那么,有人知道为什么会发出这个警告吗?我什至没有在我的代码中的任何地方使用 C。我使用的只是<random>
,它本身不是 C(但可能包括一个"C"库,谁知道呢?
无论如何,有没有办法在编译时检测需求并取消定义major
(例如,使用一些预处理器巫毒教)?
更新:我使用的是<random>
,而不是<cstdlib>
。我刚刚发现<cstdlib>
也会触发警告。
取消定义宏是可以的,这里只是为了向后兼容,很快就会被删除,并且一开始就不应该存在。此#undef
不可能破坏或损害用户代码。
如果您的用户需要在同一源文件中包含的宏和标头,请让他们自己整理。无论如何,无论您是否包含冒犯性的标头以及是否#undef
它,他们都必须这样做。
不幸的是,解决方案是停止尝试创建名为major
的成员。 否则,您需要#undef major
这对于库来说似乎很不愉快(大概是在头文件中)。
相关文章:
- 将成员变量添加到共享库中的类中,不会破坏二进制兼容性吗
- 对RValue对象调用的LValue ref限定成员函数
- 为什么使用 "this" 指针调用派生成员函数?
- 具有奇怪重复模板模式的派生类中的成员变量已损坏
- 助记符和指向成员语法的指针
- 用于访问容器<T>数据成员的正确 API
- 内置函数可查看CPP中的成员变量
- 是否可以初始化不可复制类型的成员变量(或基类)
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 找不到成员对象:没有名为get_event()的成员,也处理多态性和向量
- 嵌套在类中时无法设置成员数据
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 将函数类成员映射到类本身内部
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- 将Ref对象作为类成员
- 将包含C样式数组的对象初始化为成员变量(C++)
- 静态数据成员的问题-修复链接错误会导致编译器错误
- 将公共但非静态的成员函数与ALGLIB集成
- 多成员Constexpr结构初始化
- G++ 不允许我定义名为 "major" 的成员函数