I've got a question for all of the veteren C++ programmers out there.... I know that in_addr_t is a byte addressed value (representing an IPv4 address) however in debugging this code I was expecting a compile error ( scope of ipaddr declared in if statement would not extend to the else block):
if( in_addr_t ipaddr = inet_addr( s ) == -1 )
{
gpstk::Exception ie( "invalid IPv4 inet_addr : " + s );
GPSTK_THROW(ie);
}
else
{
inetList.push_back( ipaddr );
}
Instead, what I found is that this code compiles fine but instead pushes zero values onto inetList for every valid IPv4 address ( s == "128.1.1.1" for instance).
If I parenthesize ( in_addr_t ipaddr = inet_addr( s ) )
then I will get the expected compile error and if I declare in_addr_t ipaddr;
outside of the if-else statement then everything works as expected... truth be told I understand the fixes a bit more than the problem itself as it seems to me that the declaration and assignment of in_addr_t ipaddr = inet_addr( s )
should either be out of scope (and thus result in a compile error) or should work correctly; however not incorrectly represent the value of ipaddr
as zero.
Any thoughts?