- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
以下程序使用 -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)
感谢@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
物化为临时对象,该对象绑定(bind)到参数 x
。但是当构造函数退出时,该临时对象的生命周期结束,在评估 foo.x_
时将 x_
作为悬空引用。
您需要提供更多关于您希望您的 CHECK
宏如何工作的详细信息,然后才能建议一种无需执行您在此处所做的操作即可实现它的方法。
关于c++ - 警告 : ‘<anonymous>’ is used uninitialized in this function [-Wuninitialized],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55976434/
我想问一个让我困惑的问题。我正在尝试扫描字符串并将其转换为实数。使用该数字来计算值(value)。这是我的代码: string input_file_name1 = "shen_test_38_30_
我正在为我的 C 类入门编写一个程序,当我尝试使用 gcc 进行编译时,我不断收到一些警告。 这是我的代码: char **outList; *outList = strdup(cloudDevice
我正在使用 Chromium 嵌入式框架。我将以下内容放在主函数中。 CefRefPtr cef; CefRequest::ReferrerPolicy origin = origin; cef->S
我是C++的新手,正在测试while循环以及C++的绝对速度及其对我的CPU的影响,但出现以下错误: Severity Code Description Project File Line Suppr
string foo; try { foo = "test"; // yeah, i know ... } catch // yeah, i know this one too :) {
当尝试在 Mac OS X 10.6 上使用 FFMPEG gem 时,ruby 会抛出一个 NameError 异常,如下所示: NameError: uninitialized constant
我做了一个程序来计算 View 的总宽度/高度(有时我想要总宽度,有时我想要总高度)。唯一的问题是:如果我正在计算宽度,我想添加一个额外的 10到总数。这是我当前的代码: func calculate
所以我只想从常规地址(字符串)中提取经度/纬度坐标。我查阅了 geokit gem 文档并按照记录的内容进行了操作,但我不断收到此错误:“NameError:未初始化的常量 Geokit::Geoco
为什么 perl -we '$c = $c+3' 上升 Use of uninitialized value $c in addition (+) at -e line 1. perl -we '$c
我有以下代码: class circularList { public: circularList() : data(0), next(this) {} public: int dat
我的情况 我正在 ASP.NET MVC4 应用程序中进行测试。我正在开发的应用程序部分将现代 WebSecurity/SimpleMembershipProvider 与正在逐步淘汰的遗留身份验证系
我是 Ruby on Rails 的新手,我想使用迁移生成 mysql 数据库。 我尝试过这个命令 ruby bin/rake db:drop db:create db:migrate --trace
我需要一些调试帮助,因为我遇到的错误真的很难。 这是一款具有复杂动画的游戏。然而,问题不在于 SpriteKit .我希望动画按照严格的顺序彼此跟随,所以我实现了 Operation 的子类: cla
这是一个示例代码: #include int main() { int n = 5; float v[n]; float sum; int i; for(i
我正在使用 SQL Server 2012 并尝试实现事务复制。我正在使用系统存储过程来创建发布和订阅。我成功地创建了这些东西,但是当我检查复制监视器时,它显示“未初始化的订阅”。 当我检查订阅的同步
我有以下脚本: use 5.12.4; use strict; use warnings; say "Enter a functionality:"; while (<>) { if (/ad
我已经阅读了有关 has_many 的文档和大量教程:通过 Rails 中的关系,但我终生无法掌握它的窍门。 我正在尝试向我的 current_user(devise) 添加一个组,并且我在 Grou
我正在尝试为移动 API 设置路由,它应该有一个版本化的 api-path。我已经可以让移动 Auth 工作了,这是在位于 的单独 Controller AuthController 中实现的。/co
我正在使用 luacheck(在 Atom 编辑器中),但对其他静态分析工具开放。 有没有办法检查我是否使用了未初始化的表字段?我阅读了文档( http://luacheck.readthedocs.
我有一家工厂,例如: FactoryGirl.define do factory :page do title 'Fake Title For Page' end end 并进行测试:
我是一名优秀的程序员,十分优秀!