# How do i get rid of this warning?

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

## Recommended Posts

Im doing socket programming. After including the file <Ws2tcpip.h> for access to the function getaddrinfo(), i get the following warning: WSPiApi.h(922) : warning C4127: conditional expression is constant The code on that line is a: while (FALSE); Ok fine, but i dont want any warnings in my otherwise clean code. And i cant change in a standard header. So i #pragma it out! But!
#pragma warning(push)
#pragma warning(disable : 4727)

#include <WinSock2.h>
#include <Ws2tcpip.h>

#pragma warning(pop)


The above code does no eliminate the warning. This is the only place with the include of that file (its a precompiled stdafx header). Anyone got any clue? There are other warnings in that file that goes away with "pragma warning(disable)". But not this one. Help!

##### Share on other sites
Well, first off, the warning is 4127 yet you pragma out 4727. This could be avoided by using a for loop instead of a while loop, as explained here, but as you mentioned, you dont want to change a standard header. I would just disable the warning (using the correct number) and move on :)

##### Share on other sites
Check the actual warning code, and compare it to the code that is in your pragma statement [grin]

EDIT: Doggan was faster :)

##### Share on other sites
Ooooohhh, doh! thanks :D

##### Share on other sites
Why is it complaining about that value being a constant? what's wrong with that?

##### Share on other sites
Quote:
 Original post by NImWhy is it complaining about that value being a constant? what's wrong with that?

Because the while loop's body is never or always executed when the condition is a constant (never executed in this case). If the condition is constant, you are most likely misusing the loop concept, you are supposed to use the condition to break out of the loop.

Also I don't really see the point of:
while(FALSE);
It doesn't have a body, and if it had it would never be executed.

EDIT: Wow, thanks bakery2k1, my eyes hurt now. I thought it had to do with the expansion of a macro, with is quite bad for constructing loops, but it turned out to be much worse.

[Edited by - CTar on October 22, 2006 8:42:35 AM]

##### Share on other sites
Quote:
 Original post by CTarAlso I don't really see the point of:while(FALSE);It doesn't have a body, and if it had it would never be executed.

The OP did not completely describe the line mentioned - it is actually:
} while(FALSE);
This is the end of a do-while loop, which is being used, it appears, to allow the use of break within the loop body:
do{...if(...)    break;...} while(FALSE);
I don't see why anyone would do this. Working to a crazy coding standard mandating a maximum number of nested if statements and "NO GOTOS!!!111"?

In later versions of the file, it is replaced with the equally insane:
for(;;){...if(...)    break;...break;}

##### Share on other sites
There are times when you want a do{...} while(false);. I use it often in PHP; when you don't want to wrap your code in a function because of the hassle of declaring numerous globals, but you need a way to perform early-escape from a very long sequence of code, you have code like this...

success = false;
do {
// actions
if ( fail_condition )
break;

// actions
for ( iteration ) {
if ( fail_condition )
break 2;
// actions
}

// etc

success = true;
} while ( false );

I use this kind of construct to do validation and massive actions in PHP, like validating some mass-record alteration will work. I use this construct most often in tandem with MySQL transactions ("START", "COMMIT/ROLLBACK") to get the error safety I need.

##### Share on other sites
Quote:
 Original post by WyrframeThere are times when you want a do{...} while(false);. I use it often in PHP; when you don't want to wrap your code in a function because of the hassle of declaring numerous globals, but you need a way to perform early-escape from a very long sequence of code, you have code like this...

I have never worked with PHP, but in C++ and all other imperative languages with proper control structures I know of, it's considered very bad practice to do something like that. There aren't times you want to do it! It's just a hidden goto, so people think they are writing good code, a much better alternative would be:
f();if( failed() )  goto failed_label;g();failed_label:h();

do{  f();  if( failed() )    break;  g();}while(false);h();

The code still contains the original design flaw, but it isn't hidden anymore. No need to hide it, you should just fix it.

##### Share on other sites
I don't think I've ever seen anyone suggest using a goto. (Shame on you[grin])
If it is unlikely to need to break out most of the time, then it is the perfect place to use an exception.
Otherwise, the code up until where it breaks to should be put in a seperate function and then a return will replace the break.

Back on topic. I actually disable warning 4244 project-wide, but it still gives me some of those warnings anyway. Anyone else get that?

• 17
• 11
• 12
• 9
• 49
• ### Forum Statistics

• Total Topics
631395
• Total Posts
2999757
×