简体   繁体   中英

What is the correct way to avoid c++ compiler's warning?

How can I remove annoying warning "non-void function does not return a value in all control paths"?

There are some explanation of my code:

  1. Poll two sockets.
  2. a) If there any data on transport socket -> recv and return message from function
    b) If there any data on inproc socket -> recv and forward message from inproc socket to transport socket and continue to poll.
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
        }
    }
}

From the code, warning is legit.

when m_poller.poll() returns false , function reach end of non-void function without return.

So you have to return (or throw or call a no-return function as abort ) after the loop.

if m_poller.poll() cannot return false (due to a hidden dependency), you might rewrite your loop (and so avoid unneeded condition)

while (true) {
    m_poller.poll();
// ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM