开发者

C++ rvalue expression with destructor generates warning C4701 in Visual Studio 2010

The following C++ code, compiles without warning in Visual Studio 2010:


extern void callFunc( int, int );

struct str_wrapper
{
 str_wrapper();
};

extern bool tryParseInt( const str_wrappe开发者_如何学编程r& str, int& outValue );

void test()
{
 int x, y;
 if ( tryParseInt( str_wrapper(), x ) && tryParseInt( str_wrapper(), y ) )
 {
  // No warning generated
  callFunc( x, y );
 }
}

However if str_wrapper has a user-defined destructor, the code generates the warning on the callFunc(x, y) line:

    warning C4701: potentially uninitialized local variable 'y' used.


extern void callFunc( int, int );

struct str_wrapper { str_wrapper(); ~str_wrapper(); ///< Causes warning C4701 below };

extern bool tryParseInt( const str_wrapper& str, int& outValue );

void test() { int x, y; if ( tryParseInt( str_wrapper(), x ) && tryParseInt( str_wrapper(), y ) ) { // C4701 generated for following line callFunc( x, y ); } }

I would be happy if both examples generated a warning, or if neither example generated a warning. Am I missing some obscure C++ rule, or is this a compiler bug?


Unfortunately I wasn't able to reproduce that warning with provided code. However, I assume the compiler generates it because of a short-circuit evaluation language feature.

X should be always "initialized" in tryParseInt function, but "initialization" of Y depends only on boolean result from previous tryParseInt(str_wrapper(), x) call. But yeah, it still doesn't make any sense why the warning was generated for line inside an if-block. Maybe the compiler has fooled itself?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜