![](/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.