gpt4 book ai didi

c++ - 编译器不会提示函数没有返回值

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:25:35 32 4
gpt4 key购买 nike

我有以下功能:

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()

这编译得很好,但是编译器不应该提示缺少 return 语句吗?

编辑 0:GNU g++ 编译器

最佳答案

尝试使用 -Wall 选项进行编译 (gcc)[ -Wreturn-type 更精确]。您会收到类似“控制到达非 void 函数的末尾”或类似“返回非 void 的函数中没有返回语句”的警告

例子:

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>

使用非 void 函数(没有 return 语句)的返回值是未定义的行为。

关于c++ - 编译器不会提示函数没有返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4290473/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com