Jump to content
  • Advertisement
Sign in to follow this  
Boder

Why does this compile? C++

This topic is 4565 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

This really messed me up in my project. The below is an example.
int x() {}
int main() { int y = x(); }

Share this post


Link to post
Share on other sites
Advertisement
It compiles because the standard doesn't say it's an error and compilers need to compile code which isn't erroneous in order to be standards-compliant. It's also occasionally useful:


int a()
{
if(b) {
return 3;
} else {
exit(-1);
}
}


All modern compilers will recognize code where not all the paths return a value, and warn you about it. Programmers who ignore warnings--or lower the warning level--do so at their own peril.

Share this post


Link to post
Share on other sites
It only warns if I use "-Wall" is this normal or should the compiler be warning me by default?

When a function returns a class, will that class be properly constructed (with a default constructor) or will the returned object be undefined?

Share this post


Link to post
Share on other sites
Quote:
Original post by Boder
It only warns if I use "-Wall" is this normal or should the compiler be warning me by default?


Warning levels (which is what -Wall effectively is) are compiler-dependent. There is nothing which says that this code must produce a warning at all.

Anyway, I recommend always using -Wall.

Share this post


Link to post
Share on other sites
Quote:
Original post by Boder
It only warns if I use "-Wall" is this normal or should the compiler be warning me by default?


You should be using -Wall all the time.

Share this post


Link to post
Share on other sites
Quote:
Original post by Sneftel
It compiles because the standard doesn't say it's an error and compilers need to compile code which isn't erroneous in order to be standards-compliant. It's also occasionally useful:


int a()
{
if(b) {
return 3;
} else {
exit(-1);
}
}


All modern compilers will recognize code where not all the paths return a value, and warn you about it. Programmers who ignore warnings--or lower the warning level--do so at their own peril.


To avoid this warning, you can do this:


int a()
{
if(!b) {
exit(-1);
}

return 3;
}


Any "non return" exits before end of function.

Share this post


Link to post
Share on other sites
Yes. That can often require ordering your logic in an unintuitive fashion, though. Luckily, compilers tend to recognize functions like exec() and exit() as "special", and don't emit the warning. Of course, you can always just put a "return 0" right afterwards.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!