次のコードがあるとします。
#if !defined(__cplusplus)
# error This file should be compiled as C++
#endif
#include <stdio.h>
#include <string>
//#define USE_CXX_CLASS
#ifdef USE_CXX_CLASS
class SomeClass
{
public:
SomeClass() {}
~SomeClass() {}
std::string GetSomeString()
{
// case #1
}
};
#endif // USE_CXX_CLASS
int foo()
{
// case #2
}
int
main (int argc, char *argv[])
{
(void)argc;
(void)argv;
#ifdef USE_CXX_CLASS
SomeClass someInstance;
someInstance.GetSomeString();
#endif // USE_CXX_CLASS
foo();
return 0;
}
また、GCC バージョン 4.2.1 から options を使用して C++ コンパイラ (C コンパイラではなく) をコンパイルするとします-Wreturn-type -Werror=return-type
。上記の行のコメントを解除せずに上記のコードをそのままコンパイル//#define USE_CXX_CLASS
すると、警告は表示されますが、エラーは表示されません。
.../gcc-4.2.1/bin/g++ -g -fPIC -Wreturn-type -Werror=return-type test.cpp -c -o test.o
test.cpp: In function 'int foo()':
test.cpp:26: warning: control reaches end of non-void function
しかし、//#define USE_CXX_CLASS
行がコメント解除されている場合、警告はエラーとして扱われます:
.../gcc-4.2.1/bin/g++ -g -fPIC -Wreturn-type -Werror=return-type test.cpp -c -o test.o
test.cpp: In member function 'std::string SomeClass::GetSomeString()':
test.cpp:18: error: no return statement in function returning non-void [-Wreturn-type]
gmake: *** [test.o] Error 1
はい、1 つは非メンバー関数 (ケース #2) で、もう 1 つは C++ 関数 (ケース #1) です。IMO、それは問題ではありません。両方の条件をエラーとして扱いたいのですが、現時点では-Werror
orを追加したくありません(おそらく後で追加する予定ですが、それはこの質問の範囲外です)。-Wall
私のサブ質問は次のとおりです。
- 私が見逃しているGCCスイッチが機能するはずですか?
#pragma
(いいえ、私は'sを使いたくありません。) - これは、GCC の最新バージョンで対処されたバグですか?
参考までに、私はすでに次のような他の同様の質問に注いでいます。