【发布时间】:2017-02-06 08:01:15
【问题描述】:
使用std::ignore 忽略未使用的变量是一种好方法吗?
假设我有这样的功能:
void func(int i)
{
//for some reason, I don't need i anymore but I cannot change signature of function
std::ignore = i;
}
其他信息
这是一个例子,一些答案建议使用匿名变量。但是对于其他情况,我该怎么做,例如:
int Thread_UnSafe_func_returnSomething():
void func()
{
// To make it thread safe
// Also it is required to call only once
static int i = Thread_UnSafe_func_returnSomething();
std::ignore = i;
}
【问题讨论】:
-
您可以将其强制转换为 void:
(void)i;以抑制 C++17 之前代码中的警告。
标签: c++ function c++11 c++14 principles