Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++11 Do all control paths still need to return a value?

This my seem like a ridiculous question but today I wrote a member function in C++ that is supposed to return and int but doesn't always. I even wrote a really simple function that doesn't return a value...

int derp()
{
    if (11 == 22) return 0;
}

Is this a recent change? is my compiler broken? lol

EDIT: this does compile btw

like image 366
David Carpenter Avatar asked Dec 20 '22 12:12

David Carpenter


1 Answers

In a non-void function all control paths must return. The key issue here is that the compiler is not required to diagnose it. Note that compile and is correct are not necessarily the same. All correct code compiles, but not all code that compiles is correct.

like image 123
David Rodríguez - dribeas Avatar answered Dec 23 '22 03:12

David Rodríguez - dribeas