- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
boost::mutex::scoped_lock
是一个方便的 RAII 包装器,用于锁定互斥锁。我对其他事情使用了类似的技术:一个 RAII 包装器,它要求数据接口(interface)从/重新连接到串行设备。
不过,我想不通的是,为什么在下面的代码中只有我的对象 mst
(其实例化和销毁 确实有副作用)会导致 g++
发出“未使用的变量”警告错误,而 l
设法保持沉默。
你知道吗?你能告诉我吗?
[generic@sentinel ~]$ cat test.cpp
#include <boost/shared_ptr.hpp>
#include <boost/thread/mutex.hpp>
#include <iostream>
struct MyScopedThing;
struct MyWorkerObject {
void a() { std::cout << "a"; }
void b() { std::cout << "b"; }
boost::shared_ptr<MyScopedThing> getScopedThing();
};
struct MyScopedThing {
MyScopedThing(MyWorkerObject& w) : w(w) {
w.a();
}
~MyScopedThing() {
w.b();
}
MyWorkerObject& w;
};
boost::shared_ptr<MyScopedThing> MyWorkerObject::getScopedThing() {
return boost::shared_ptr<MyScopedThing>(new MyScopedThing(*this));
}
int main() {
boost::mutex m;
boost::mutex::scoped_lock l(m);
MyWorkerObject w;
const boost::shared_ptr<MyScopedThing>& mst = w.getScopedThing();
}
[generic@sentinel ~]$ g++ test.cpp -o test -lboost_thread -Wall
test.cpp: In function ‘int main()’:
test.cpp:33: warning: unused variable ‘mst’
[generic@sentinel ~]$ ./test
ab[generic@sentinel ~]$ g++ -v 2>&1 | grep version
gcc version 4.4.5 20110214 (Red Hat 4.4.5-6) (GCC)
最佳答案
请注意,在写完其他答案后,问题已经发生了变化。
g++ 在当前表单中没有发出警告的原因可能是因为 mst
是一个引用,并且构造和破坏一个引用没有副作用。确实,这里的引用延长了临时对象的生命周期,这对其构造函数和析构函数都有影响,但显然 g++ 并没有意识到这会有所不同。
关于c++ - scoped_lock 如何避免发出 "unused variable"警告?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7418567/
__attribute__((unused)) 和 __attribute((unused)) 可以将变量标记为未使用以避免未使用警告。 它们有什么区别? 最佳答案 在 GCC 存储库中,在文件 c-
在 Visual Studio 2010(C++,非托管)中调试,信息 unused ??? 或 unused 0 应该是什么意思?我附上了两个屏幕截图, child 是现有窗口的 HWND。 我也对
我只想在我的 Xcode 项目的一个文件中删除此编译器警告。有办法做到这一点吗? 最佳答案 您可以使用 a pragma directive and the "diagnostic" keyword
为什么.NET 警告“未使用的变量”而不是“未使用的参数”? (我相信 Java 在这两种情况下都会发出警告。) 为什么 .NET 不关心“未使用的参数”? 最佳答案 在我看来,您可能真正想要保留未使
我正在处理这个教程应用程序,代码给我这个警告: Xcode WARNING: Unused Entity Issue: Unused Variable 执行这条语句时报错: int newRowInd
考虑以下代码: class Foo { let bar = "Hello world!" init () { self // Warning: Expression o
您好,我正在尝试将 zxing 二维码阅读器整合到我的应用程序中。我直接从谷歌代码网站检查了 svn,并在我的项目设置中添加了所有标题路径。然而,当我尝试构建项目时,我遇到了很多“未使用的函数”和“未
在 Pipes Tutorial ,它说: The concrete type synonyms use () to close unused inputs and X (the uninhabite
我有工作 Swift.2.3 项目,但是当我构建并将其转换为 Swift3 时,它给我: Result of call to (_:parameters:completionHandler:)' is
我有一个基于 cmake 的包。它有几个目标可执行文件。其中一个目标时不时会崩溃。回溯如下: ... #19 XXX::~XXX (this=0x69a120, __in_chrg=) at
我刚刚用 C 编写了我的第一个应用程序,我收到了这个警告(已编辑):unused variable pp int compteur = 1; int *p = &compteur; int **pp
我的 C++ 编译器发出警告,指出以下“intVar”变量“未使用”。 void MyClass::MyMethod(bool bFlag) { int intVar = 10; if
我正在处理一个 Flask 项目,从 API 包装器获取一些数据。包装器返回一个生成器对象,所以我在将其传递给 Flask 的 render_template() 之前打印值(for obj in g
我是 goLang 的新手。我正在尝试从 intellij 构建一个项目,该项目使用 git 库中的一个包: import ( "github.com/aerospike/aerospike-
我收到很多不正确的 ESLint/TS 警告,说枚举案例“已分配一个值但从未使用过”或导入“已定义但从未使用过”。这是一些代码。 所有导入都说它们已定义但从未使用过(尽管您可以看到它们在底部的类型中)
这对我来说真是个谜。我已经这样定义了我的方法(对于类“graf”): addStatistics <- function(x) UseMethod("addStatistics") addStatis
我正在尝试使用 grunt-unused 删除多个子目录中所有未使用的图像链接。 .为清楚起见,这是我的文件夹结构: |-- dist | |-- site-1 | | |—-index.htm
我试图在我通过函数创建的条形图上注释标准消息。以下是代码: hashbar <- function(x) { suppressWarnings(library(stringr))
我在一个过程中有一个变量,我需要保持该变量的事件状态,直到该过程中的匿名方法运行为止,但我不在匿名方法中使用该变量。有没有一种惯用的方法告诉编译器无论如何都要捕获变量? 例如: procedure F
我正在使用 Delphi XE、Windows 7。 在应用程序中,我想启用不同的报告类型供用户选择。为此,我有 1 个基本报告类和每个报告类型(xml、csv、ppt 等)的子类。 {Just an
我是一名优秀的程序员,十分优秀!