gpt4 book ai didi

带有析构函数的 C++ 右值表达式在 Visual Studio 2010 中生成警告 C4701

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:20:10 24 4
gpt4 key购买 nike

以下 C++ 代码在 Visual Studio 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 );
}
}

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

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


extern void callFunc( int, int );<p></p>

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

<p>extern bool tryParseInt( const str_wrapper& str, int& outValue );</p>

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

如果两个示例都生成了警告,或者两个示例都没有生成警告,我会很高兴。我是否遗漏了一些晦涩的 C++ 规则,或者这是一个编译器错误?

最佳答案

不幸的是,我无法使用提供的代码重现该警告。但是,我假设编译器生成它是因为短路评估语言功能。

X 在 tryParseInt 函数中应该总是被“初始化”,但是 Y 的“初始化”只依赖于之前 tryParseInt(str_wrapper(), x) 调用的 bool 结果。但是,是的,为什么为 if block 内的行生成警告仍然没有任何意义。可能是编译器自欺欺人了?

关于带有析构函数的 C++ 右值表达式在 Visual Studio 2010 中生成警告 C4701,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3249612/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com