0

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 } } } 
8
  • 7
    Just return std::nullopt after that while loop. Commented Nov 27, 2020 at 21:19
  • 1
    You may put throw in the unreachable function end. It suppresses warnings too. Commented Nov 27, 2020 at 21:22
  • 3
    ... and what is the function supposed to return when m_poller.poll() is false? Add that return statement, and program is fixed. Commented Nov 27, 2020 at 21:22
  • @nada thank you, that solved the problem ! Commented Nov 27, 2020 at 21:24
  • 1
    @exoze You're welcome. That is a very valid warning btw. Always watch out that your function returns in all its possible outcomes, else there be dragons. Compiler warnings are meant to be helpful messages to us programmers. Don't turn them off. Commented Nov 27, 2020 at 21:25

1 Answer 1

1

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(); // ... } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.