编译器不抱怨函数不返回值 [英] compiler doesn't complain about function not returning value
本文介绍了编译器不抱怨函数不返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下函数:
bool Server :: ServerInit()
{
/ / bool listenResult = socket.Listen((const uint8 *)_ LOCAL_HOST,m_iPort);
// if(true == listenResult)
// cout<< 服务器被动套接字侦听\ n;
// else
// cout<< 服务器被动套接字不侦听\\\
;
//
// return listenResult;
} // ServerInit()
这个编译完全正常,抱怨没有回报表?
编辑0:GNU g ++编译器
解决方案
code> -Wall 选项(gcc)[ -Wreturn类型
更精确]。你会得到一个类似控制到达非void函数的结束或类似函数返回非空void返回语句的警告
示例:
C:\Users\SUPER USER\Desktop> type no_return.cpp
#include< iostream>
int func(){}
int main()
{
int z = func();
std :: cout<< z; //未定义的行为
}
C:\Users\SUPER USER\Desktop> g ++ -Wall no_return.cpp
no_return.cpp:在函数'int func()':
no_return.cpp:2:12:warning:函数中没有return语句返回非空
C:\Users\SUPER USER\Desktop>
使用非空函数(没有返回语句)的返回值是未定义行为。 / p>
I have the following function:
bool Server::ServerInit()
{
// bool listenResult = socket.Listen( (const uint8 *)_LOCAL_HOST, m_iPort );
// if( true == listenResult )
// cout << "Server passive socket listening\n";
// else
// cout << "Server passive socket not listening\n";
//
// return listenResult;
} // ServerInit()
this compiles perfectly fine, but shouldn't the compiler be complaining about the absence of a return statement?
EDIT 0: GNU g++ compiler
解决方案
Try compiling with -Wall
option (gcc)[ -Wreturn-type
to be more precise]. You'll a get a warning something like "Control reaches end of a non-void function" or something like "no return statement in function returning non-void"
Example:
C:\Users\SUPER USER\Desktop>type no_return.cpp
#include <iostream>
int func(){}
int main()
{
int z = func();
std::cout<< z; //Undefined Behaviour
}
C:\Users\SUPER USER\Desktop>g++ -Wall no_return.cpp
no_return.cpp: In function 'int func()':
no_return.cpp:2:12: warning: no return statement in function returning non-void
C:\Users\SUPER USER\Desktop>
Using the returned value of a non-void function (having no return statement) is Undefined Behaviour.
这篇关于编译器不抱怨函数不返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文