C++;带析构函数的右值表达式在Visual Studio 2010中生成警告C4701 以下C++代码,在VisualStudio 2010中没有警告地编译: extern void callFunc( int, int ); struct str_wrapper { str_wrapper(); }; extern bool tryParseInt( const str_wrapper& str, int& outValue ); void test() { int x, y; if ( tryParseInt( str_wrapper(), x ) && tryParseInt( str_wrapper(), y ) ) { // No warning generated callFunc( x, y ); } }

C++;带析构函数的右值表达式在Visual Studio 2010中生成警告C4701 以下C++代码,在VisualStudio 2010中没有警告地编译: extern void callFunc( int, int ); struct str_wrapper { str_wrapper(); }; extern bool tryParseInt( const str_wrapper& str, int& outValue ); void test() { int x, y; if ( tryParseInt( str_wrapper(), x ) && tryParseInt( str_wrapper(), y ) ) { // No warning generated callFunc( x, y ); } },c++,visual-c++,expression,warnings,rvalue,C++,Visual C++,Expression,Warnings,Rvalue,但是,如果str_包装器具有用户定义的析构函数,则代码会在callFunc(x,y)行生成警告: 警告C4701:使用了可能未初始化的局部变量“y”。 外部无效调用函数(int,int) 结构stru包装器 { stru包装器(); ~str_wrapper();//

但是,如果str_包装器具有用户定义的析构函数,则代码会在callFunc(x,y)行生成警告:

警告C4701:使用了可能未初始化的局部变量“y”。

外部无效调用函数(int,int)

结构stru包装器 { stru包装器(); ~str_wrapper();//<会导致下面的警告C4701 };

外部布尔tryParseInt(常量str_包装器和str、int和outValue)

无效测试() { int x,y; if(tryParseInt(str_wrapper(),x)和&tryParseInt(str_wrapper(),y)) { //为以下行生成C4701 callFunc(x,y); } }


如果两个例子都产生了警告,或者两个例子都没有产生警告,我会很高兴。我是否缺少了一些模糊的C++规则,或者这是编译器错误?

不幸的是,我不能用提供的代码复制警告。但是,我假设编译器生成它是因为短路求值语言特性


在tryParseInt函数中,X应始终“初始化”,但Y的“初始化”仅取决于上一次tryParseInt(str_wrapper(),X)调用的布尔结果。但是的,它仍然没有任何意义,为什么警告是在一个if块内的行生成的。可能是编译器自欺欺人了?

C4701是一个4级警告,因此请确保在Visual Studio属性页中有/W4或同等版本。我同意上面的代码在风格和健壮性方面不是最好的。我主要关心的是,这在编译器级别上是否为假阳性,或者导致警告的代码中是否存在某些差异。据我所知,只有在x和y都调用了tryParseInt并且两个调用都成功的情况下才应该调用callFunc。我猜这是因为tryParseInt()接受一个int引用,如果编译器不知道tryParseInt()中发生了什么,那么它可以假设int引用没有任何变化,而不进行赋值。

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 ); } }