警告:变量已设置,但未使用
warning: variable set but not used
T offset = ps->first, prev_offset;
bool first = true;
while (1) {
if (first)
first = false;
else
assert(offset > prev_offset);
pl = l.find_inc(offset);
prev_offset = offset;
if (pl == l.m.end())
break;
while (ps != s.m.end() && ps->first + ps->second <= pl->first)
++ps;
if (ps == s.m.end())
break;
offset = pl->first + pl->second;
if (offset <= ps->first) {
offset = ps->first;
continue;
}
}
我收到"prev_offset"的[-Wunused-but-set-variable]警告,除了在prev_offset = offset;
后添加cout << prev_offset;
之外,还有更好的方法可以解决它吗?任何答案都是赞赏的,提前感谢。
有几种方法可以解决这个问题。一种是放入一个虚拟语句,将变量转换为 (void)。
(void) prev_offset; // this should stop the warning.
另一种方法是有条件地包含变量,其方式类似于assert()
如何根据是否设置了NDEBUG
宏有条件地包含其检查。
// When you use ASSERT_CODE() the code only appears in debug builds
// the same as the assert() checks. That code is removed in
// release builds.
#ifndef NDEBUG
#define ASSERT_CODE(code) code
#else
#define ASSERT_CODE(code)
#endif
// ...
T offset = ps->first;
ASSERT_CODE(T prev_offset); // give it its own line
bool first = true;
while (1) {
if (first)
first = false;
else
assert(offset > prev_offset);
pl = l.find_inc(offset);
ASSERT_CODE(prev_offset = offset);
if (pl == l.m.end())
break;
while (ps != s.m.end() && ps->first + ps->second <= pl->first)
++ps;
if (ps == s.m.end())
break;
offset = pl->first + pl->second;
if (offset <= ps->first) {
offset = ps->first;
continue;
}
}
相关文章:
- 普通环路未使用gcc 4.8.5自动矢量化
- 在更改for循环的第三部分后,未使用for循环结果
- 使用typeid警告未使用的变量
- C++删除未使用的类属性会导致 std::logic_error
- 警告 C4552:">>":未使用表达式的结果
- (SFML)按下键时,播放器构造函数未使用正确的动画进行更新
- 使用设置精度时如何阻止数字向上舍入?
- 为什么我会收到警告,指出函数已使用但未定义,以及已定义但未使用?
- 在 macOS 中使用 C/C++ 设置鼠标光标图像
- 如果我想链接静态库并删除未使用的符号.txt我应该如何处理 Cmakelist
- 可视化C++载体:未使用的容量
- GCC 中 __attribute__((未使用)) 和 __attribute((未使用)) 之间的区别
- 循环访问还包含未使用元素的字符串数组
- 参数设置但未使用
- 警告:变量已设置,但未使用
- 警告:变量“{variable}”已设置但未使用-C++
- 使用设置查找功能,未找到运算符
- GetMenuItemInfo未使用MIIM_TYPE设置fType
- 用clang捕获设置但未使用的参数
- 是否可以复制未初始化的数据,如果它以后将不使用/设置