禁用作用域防护的"Unused variable"
Disable "Unused variable" for ScopedGuard
我正在与安德烈·亚历山德雷斯库和彼得鲁·边缘安一起玩 范围警卫对象
当你用-Wall -Werror编译它时,你会得到"未使用的变量"错误。以下代码取自 LOKI
class ScopeGuardImplBase
{
ScopeGuardImplBase& operator =(const ScopeGuardImplBase&);
protected:
~ScopeGuardImplBase()
{}
ScopeGuardImplBase(const ScopeGuardImplBase& other) throw()
: dismissed_(other.dismissed_)
{
other.Dismiss();
}
template <typename J>
static void SafeExecute(J& j) throw()
{
if (!j.dismissed_)
try
{
j.Execute();
}
catch(...)
{}
}
mutable bool dismissed_;
public:
ScopeGuardImplBase() throw() : dismissed_(false)
{}
void Dismiss() const throw()
{
dismissed_ = true;
}
};
////////////////////////////////////////////////////////////////
///
/// typedef typedef const ScopeGuardImplBase& ScopeGuard
/// ingroup ExceptionGroup
///
/// See Andrei's and Petru Marginean's CUJ article
/// http://www.cuj.com/documents/s=8000/cujcexp1812alexandr/alexandr.htm
///
/// Changes to the original code by Joshua Lehrer:
/// http://www.lehrerfamily.com/scopeguard.html
////////////////////////////////////////////////////////////////
typedef const ScopeGuardImplBase& ScopeGuard;
template <typename F>
class ScopeGuardImpl0 : public ScopeGuardImplBase
{
public:
static ScopeGuardImpl0<F> MakeGuard(F fun)
{
return ScopeGuardImpl0<F>(fun);
}
~ScopeGuardImpl0() throw()
{
SafeExecute(*this);
}
void Execute()
{
fun_();
}
protected:
ScopeGuardImpl0(F fun) : fun_(fun)
{}
F fun_;
};
template <typename F>
inline ScopeGuardImpl0<F> MakeGuard(F fun)
{
return ScopeGuardImpl0<F>::MakeGuard(fun);
}
问题出在用法上:
ScopeGuard scope_guard = MakeGuard(&foo);
这只是
const ScopeGuardImplBase& scope_guard = ScopeGuardImpl0<void(*)()>(&foo);
我使用宏在应对结束时获得一些操作:
#define SCOPE_GUARD ScopedGuard scope_guard = MakeGuard
这样,用户只需调用
SCOPE_GUARD(&foo, param) ...
此宏使禁用未使用的警告变得困难。
有人可以帮助我更好地理解这一点,并在不使用 -Wno-unused-variable 的情况下提供解决方案吗?
您可以尝试旧方法:
(void)scope_guard;
相关文章:
- C++ Singleton - Prevent ::instance() to variable
- 如何通过多类"Union variable" (sfml) 使用轮询事件
- 将成员函数作为构造函数参数调用时出错 "Variable is not a type name"
- 为什么我会收到"Run-Time Check Failure #2 - Stack around the variable 'pr' was corrupted"错误?
- 叮当声:折叠表情和"expression result unused"警告
- C++:寻找"returning address of local variable..."的更正
- Win32 API - HWND "{unused = ???} Unable to read memory"错误
- C++ - 在我尝试制作一个简单的计算器时有一个"uninitialized local variable y used"警告
- Visual Studio Code "variable " u8 的 C/C++ 扩展名 " " 不是类型名称"
- C++ "Using Uninitialized Memory.. (variable name) "
- cudaMallocManaged for host-initiated variable
- Cppcheck "Reference to auto variable returned"失败的原因?
- OpenCV 和 C++:"Can't resolve variable 'Mat`"
- 是否有用于"go to variable type definition" C++中"auto"变量的 Visual Studio 键盘快捷方式?
- 冲刺错误"variable may be unsafe" (C4996)...选择?
- Visual Studio生成"Error: uninitialized local variable 'x' "而在线编译器不会 - 为什么?
- GUID variable for JPEG,BMP,PNG
- 如何修复初始化后'stack around variable was corrupted.'变量未更改为分配的值
- 禁用作用域防护的"Unused variable"
- 从C++中删除"unused variable"警告