【发布时间】:2019-05-03 20:04:43
【问题描述】:
以下程序编译时没有警告-O0:
#include <iostream>
struct Foo
{
int const& x_;
inline operator bool() const { return true; }
Foo(int const& x):x_{x} { }
Foo(Foo const&) = delete;
Foo& operator=(Foo const&) = delete;
};
int main()
{
if (Foo const& foo = Foo(3))
std::cout << foo.x_ << std::endl;
return 0;
}
但是-O1 或更高版本会发出警告:
maybe-uninitialized.cpp: In function ‘int main()’:
maybe-uninitialized.cpp:15:22: warning: ‘<anonymous>’ is used uninitialized in this function [-Wuninitialized]
std::cout << foo.x_ << std::endl;
如何使用-O1 及更高版本消除此警告?
这样做的动机是 CHECK(x) 宏必须捕获 const 引用而不是值,以免触发析构函数、复制构造函数等以及打印出值。
分辨率在底部
编辑:
$ g++ --version
g++ (GCC) 8.2.1 20181127
No warnings: g++ maybe-uninitialized.cpp -Wall -O0
With warning: g++ maybe-uninitialized.cpp -Wall -O1
编辑 2 以回应 @Brian
#include <iostream>
struct CheckEq
{
int const& x_;
int const& y_;
bool const result_;
inline operator bool() const { return !result_; }
CheckEq(int const& x, int const &y):x_{x},y_{y},result_{x_ == y_} { }
CheckEq(CheckEq const&) = delete;
CheckEq& operator=(CheckEq const&) = delete;
};
#define CHECK_EQ(x, y) if (CheckEq const& check_eq = CheckEq(x,y)) \
std::cout << #x << " != " << #y \
<< " (" << check_eq.x_ << " != " << check_eq.y_ << ") "
int main()
{
CHECK_EQ(3,4) << '\n';
return 0;
}
上面更有趣的是没有警告,但根据-O0或-O1的不同输出:
g++ maybe-uninitialized.cpp -O0 ; ./a.out
Output: 3 != 4 (3 != 4)
g++ maybe-uninitialized.cpp -O1 ; ./a.out
Output: 3 != 4 (0 != 0)
编辑 3 - 接受的答案
感谢@RyanHaining。
#include <iostream>
struct CheckEq
{
int const& x_;
int const& y_;
explicit operator bool() const { return !(x_ == y_); }
};
int f() {
std::cout << "f() called." << std::endl;
return 3;
}
int g() {
std::cout << "g() called." << std::endl;
return 4;
}
#define CHECK_EQ(x, y) if (CheckEq const& check_eq = CheckEq{(x),(y)}) \
std::cout << #x << " != " << #y \
<< " (" << check_eq.x_ << " != " << check_eq.y_ << ") "
int main() {
CHECK_EQ(f(),g()) << '\n';
}
输出:
f() called.
g() called.
f() != g() (3 != 4)
特点:
-
CHECK_EQ的每个参数只检查一次。 - 输出显示内联代码比较以及值。
【问题讨论】:
-
无法重现 - 请提供您使用的完整命令行。
-
你使用的是哪个编译器?
-
我很惊讶您没有收到来自非优化构建的警告。
Foo(3)永远不会正确,因为类成员引用不会延长临时对象的寿命。你能用你的宏展示你实际想要完成的事情吗?我们应该能够提供帮助。我会试试看是否有一个欺骗目标。 -
@RyanHaining 非常好。我不知道聚合的行为方式。 TIL :)(顺便说一句,答案+1)
-
我忘了说,
inline对于在类体中定义成员函数是不必要的,这里的operator bool隐含为inline
标签: c++