bgo*_*odr 5 c++ gcc return-value gcc-warning gcc4
假设我们有以下代码:
#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;
}
Run Code Online (Sandbox Code Playgroud)
并且假设它是使用选项从GCC版本4.2.1编译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
Run Code Online (Sandbox Code Playgroud)
但是如果该//#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
Run Code Online (Sandbox Code Playgroud)
是的,一个是非成员函数(案例#2),另一个是C++函数(案例#1).IMO,这应该不重要.我希望将这两个条件视为错误,并且我不想在此时添加-Werror或-Wall(稍后可能会这样做,但这超出了此问题的范围).
我的子问题是:
#pragma.)作为参考,我已经倾注了其他类似的问题,包括以下内容:
| 归档时间: |
|
| 查看次数: |
1771 次 |
| 最近记录: |