如何处理由空模板参数包扩展引起的未使用的警告? [英] How to handle unused warnings caused by empty template parameter pack expansions?
问题描述
我仍然面临的一个问题是编译器会抱怨一个未使用的变量,即使使用了该变量,但它只能在参数包扩展中使用,该参数包扩展在特定实例化中恰好为空。
例如:
An issue I keep facing is one where the compiler complains about an unused variable, even though the variable is used, but it's only used inside a parameter pack expansion that happens to be empty for a specific instantiation. For example:
template <std::size_t... I>
auto func1(std::index_sequence<I...>)
{
auto var = get_tuple();
return func2(std::get<I>(var)...);
}
auto a = func1(std::make_index_sequence<0>());
请参阅现场示例(尝试在第4行更改元组,在内部添加一个int<>以查看警告消失)。
我知道我可以添加一个(void)var;
行来让警告消失,但是对我来说感觉很脏,特别是当函数实际上只是一条线。
我也不想全局禁用此警告,因为它有时会提供洞察力。
See live example (try changing the tuple at line 4, by adding an int inside <> to see the warning go away).
I know I could add a (void)var;
line to make the warning go away, but it feels dirty to me, especially when the function is actually just a single line.
I also don't want to disable this warning globally, because it does provide insight sometimes.
这个问题的一个类似表现是使用变量时在lambda捕获。在这种情况下,gcc没有警告,叮当抱怨(我认为海湾合作委员会从未实施警告关于未使用的lambda捕获):
A similar manifestation of this issue is when the variable is used in a lambda capture. In this case, gcc spits no warning, while clang complains (I think gcc never implemented a warning about unused lambda captures):
template <std::size_t... I>
auto func1(std::index_sequence<I...>)
{
auto var = get_tuple();
auto my_lambda = [var](){
return func2(std::get<I>(var)...);
};
return my_lambda();
}
auto a = func1(std::make_index_sequence<0>());
推荐答案
如果您可以使用C ++ 17, [ [maybe_unused]]
属性是最清晰的解决方案IMO:
If you can use C++17, the [[maybe_unused]]
attribute is the clearest solution IMO:
[[maybe_unused]]
auto tuple = get_tuple();
这篇关于如何处理由空模板参数包扩展引起的未使用的警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!