![](/img/trans.png)
[英]Compiler warning on “Effective C++”s method to avoid duplication in const and non-const member functions
[英]What is the correct way to avoid c++ compiler's warning?
如何删除烦人的警告“非无效 function 不会在所有控制路径中返回值”?
我的代码有一些解释:
std::optional<std::pair<std::string, std::string> > pizza::transport::SharedAsyncPoller::receive()
{
// poll both sockets and return if there
while (m_poller.poll())
{
// if any data appears on transport socket
if (m_poller.has_input(m_transportSocket))
{
zmqpp::message msg;
m_transportSocket.receive(msg);
if (msg.parts() < 2)
return std::nullopt;
else
return std::make_pair(msg.get(0), msg.get(1));
}
// or if there any data on inproc socket
if (m_poller.has_input(m_inprocSocket))
{
zmqpp::message msg;
m_inprocSocket.receive(msg);
m_transportSocket.send(msg);
// it is okay that we do not return anything
// we just forward the message from inproc to transport socket
// and continue to poll sockets
}
}
}
从代码来看,警告是合法的。
当m_poller.poll()
返回false
时,function 到达非 void function 的末尾而不返回。
所以你必须在循环之后return
(或throw
或调用不返回 function 作为abort
)。
如果m_poller.poll()
不能返回 false (由于隐藏的依赖关系),你可能会重写你的循环(从而避免不需要的条件)
while (true) {
m_poller.poll();
// ...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.