热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

G++不允许我定义名为"major"的成员函数

如何解决《G++不允许我定义名为"major"的成员函数》经验,为你挑选了1个好方法。

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

这是一个最低限度可行的测试片段:

// Any of these includes trigger the warnings
#include 
#include 

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 . For historical compatibility, it is
 currently defined by  as well, but we plan to
 remove this soon. To use "major", include 
 directly. If you did not intend to use a system-defined macro
 "major", you should undefine it after including .
  void major() const;
             ^~~~~~~~                                                                                                                                                                                                                                                                                                                                                                
test-gcc-major.cpp:10:13: warning: In the GNU C Library, "major" is defined
 by . For historical compatibility, it is
 currently defined by  as well, but we plan to
 remove this soon. To use "major", include 
 directly. If you did not intend to use a system-defined macro
 "major", you should undefine it after including .
 inline void my_class::major() const {}
             ^~~~~~~~~~~~~~~~~~~~~~~~~~                                                                                                                                                                                                                                                                                                                                              
test-gcc-major.cpp:14:13: warning: In the GNU C Library, "major" is defined
 by . For historical compatibility, it is
 currently defined by  as well, but we plan to
 remove this soon. To use "major", include 
 directly. If you did not intend to use a system-defined macro
 "major", you should undefine it after including .
  my_obj.major();

以任何方式触发引用成员函数的每一行的警告.此外,我无法取消任何内容,因为我正在实施一个库,这个负担应归入最终用户.

那么,有人知道为什么要提出这个警告吗?我甚至没有在我的代码中的任何地方使用C. 我正在使用的是,本身不是C(但可能包括"C"库,谁知道?).

在任何情况下,有没有办法major在编译时检测需要和取消定义(例如,使用一些预处理器巫毒)?

更新:我正在使用,而不是.我刚刚发现它也会触发警告.



1> n. 'pronouns..:

可以取消定义宏,这里仅用于向后兼容,很快就会删除,并且不应该首先出现在那里.这#undef无法破坏或损害用户代码.

如果您的用户需要宏您的标题包含在同一源文件中,请让他们自己排序.无论如何,无论你是否包含违规标题,无论你是否包含它们,他们都必须这样做#undef.


推荐阅读
author-avatar
风冷泻千山
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有