2017-09-11 7 views
3

私はいくつかの単体テストをコーディングしていましたが、突然G ++は予期せぬ警告をGNU Cとmajorというメンバ関数の1つに与えました。なぜ、G ++をトリガーせずに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を使用して):

[[email protected] ~]$ 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の定義を解除する方法はありますか(一部のプリプロセッサブードーなど)。

更新日:<cstdlib>ではなく、<random>を使用しています。私はちょうど<cstdlib>が警告を引き起こすことを知りました。

+3

エラーは自明ですが、私は言うでしょう。これは 'sys/sysmacros.h'のマクロです。あなたは幸運なことに、GCCはコードをコンパイルするのではなく、起こりそうな奇妙なことを頭で傷つけるのではなく、警告します。 – StoryTeller

+0

エラーメッセージは完全に良い英語で問題を説明しています。また、新しいC++コードで 'random'を使うべきではありません。 –

+0

@ n.m私はを使用しています。もっと慎重に投稿を読んでください。 –

答えて

5

ここでは下位互換性のためにマクロを定義解除しても問題ありません。間もなく削除されます。最初はそこにあってはいけません。この#undefがユーザコードを壊したり害したりする可能性はありません。

ユーザーがマクロのヘッダーを同じソースファイルに含める必要がある場合は、それらのファイルを並べ替えるようにしてください。とにかく、違反のヘッダーを含めるかどうか、そしてあなたが#undefかどうかに関わらず、彼らはどうにかしなければなりません。

1

残念ながら解決策は、majorというメンバーの作成をやめることです。それ以外の場合は、#undef majorにする必要があります。これは、ライブラリが(おそらくヘッダーファイル内で)行うのはかなり不愉快だと思われます。

+0

私はこの定義を必要とするクライアントを壊すことなくこの警告を回避する機能フラグまたは '#define'があることを期待していました。できるだけ非侵入的である方が良いかもしれないので、関数の名前を変更するのが最善の方法です。これはかなり不幸です。なぜなら、 'major'はその機能のために私のドメイン内でもっとも良い名前だからです。 –

関連する問題