【问题标题】:G++ doesn't allow me to define a member function named "major"G++ 不允许我定义一个名为“major”的成员函数
【发布时间】:2017-09-11 06:03:11
【问题描述】:

所以,今天我正在编写一些单元测试,突然 G++ 给了我一个关于 GNU C 和我的一个名为 major 的成员函数的意外警告。为什么我不能有一个名为major 的成员函数而不触发G++?

这是一个最低限度可行的测试 sn-p:

// 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。我使用的是&lt;random&gt;,它本身不是C(但可能包含一个“C”库,谁知道呢?)。

无论如何,有没有办法在编译时检测到需要并取消定义major(例如,使用一些预处理器巫术)?

更新:我使用的是&lt;random&gt;,而不是&lt;cstdlib&gt;。我刚刚发现&lt;cstdlib&gt;也触发了警告。

【问题讨论】:

  • 错误是不言自明的,我会说。这是sys/sysmacros.h 中的一个宏。你很幸运,GCC 会警告你,而不是仅仅编译代码并让你对即将发生的奇怪事情摸不着头脑。
  • 错误信息用非常好的英语解释了问题。您也不应该在新的 C++ 代码中使用 random
  • @n.m 我正在使用 。请仔细阅读帖子。
  • @StoryTeller 问题是是否有任何方法可以在编译时检查这一点,而不是取消定义其他编译器中未定义的内容。是否有一系列我应该关注的版本?
  • “函数被重命名”是的,这就是问题所在。您的 glibc 太旧并且不包含弃用警告,它只是默默地破坏了代码。

标签: c++ g++ libc gcc-warning libstdc++


【解决方案1】:

取消定义宏是可以的,这里只是为了向后兼容,很快就会被删除,而且一开始就不应该在那里。这个#undef 不可能破坏或损害用户代码。

如果您的用户需要宏您的标头包含在同一个源文件中,让他们自己整理。无论如何,他们都必须这样做,无论您是否包含有问题的标题以及您是否 #undef 它。

【讨论】:

    【解决方案2】:

    不幸的是,解决方案是停止尝试创建一个名为major 的成员。否则你需要#undef major,这对于库来说似乎很不愉快(可能在头文件中)。

    【讨论】:

    • 我希望有一个功能标志或#define 可以避免此警告,而不会破坏需要此定义的客户端。尽可能不打扰也许更好,所以重命名函数是要走的路。这很不幸,因为major 是我域中用于该功能的最佳名称。
    猜你喜欢
    • 2011-11-17
    • 1970-01-01
    • 1970-01-01
    • 2011-10-30
    • 2017-05-27
    • 1970-01-01
    • 2019-05-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多