c++11中未使用的参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15763937/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 19:45:17  来源:igfitidea点击:

Unused parameter in c++11

c++c++11unused-variables

提问by inkooboo

In c++03 and earlier to disable compiler warning about unused parameter I usually use such code:

在 c++03 及更早版本中,为了禁用有关未使用参数的编译器警告,我通常使用这样的代码:

#define UNUSED(expr) do { (void)(expr); } while (0)

For example

例如

int main(int argc, char *argv[])
{
    UNUSED(argc);
    UNUSED(argv);

    return 0;
}

But macros are not best practice for c++, so. Does any better solution appear with c++11 standard? I mean can I get rid of macros?

但是宏不是 C++ 的最佳实践,所以。c++11标准是否出现了更好的解决方案?我的意思是我可以摆脱宏吗?

Thanks for all!

谢谢大家!

采纳答案by MadScientist

I have used a function with an empty body for that purpose:

为此,我使用了一个带有空体的函数:

template <typename T>
void ignore(T &&)
{ }

void f(int a, int b)
{
  ignore(a);
  ignore(b);
  return;
}

I expect any serious compiler to optimize the function call away and it silences warnings for me.

我希望任何认真的编译器都可以优化函数调用,并为我消除警告。

回答by Henrik

You can just omit the parameter names:

您可以省略参数名称:

int main(int, char *[])
{

    return 0;
}

And in the case of main, you can even omit the parameters altogether:

在 main 的情况下,您甚至可以完全省略参数:

int main()
{
    // no return implies return 0;
}

See "§ 3.6 Start and Termination" in the C++11 Standard.

请参阅 C++11 标准中的“§ 3.6 开始和终止”。

回答by Tomilov Anatoliy

There is the <tuple>in C++11, which includes the ready to use std::ignoreobject, that's allow us to write (very likely without imposing runtime overheads):

还有就是<tuple>C ++ 11,其中包括准备使用std::ignore对象,这让我们写(很可能不附加任何运行时开销):

void f(int x)
{
    std::ignore = x;
}

回答by Lightness Races in Orbit

Nothing equivalent, no.

没有什么等价的,不。

So you're stuck with the same old options. Are you happy to omit the names in the parameter list entirely?

所以你坚持使用相同的旧选项。您是否乐意完全省略参数列表中的名称?

int main(int, char**)

In the specific case of main, of course, you could simply omit the parameters themselves:

main当然,在 的特定情况下,您可以简单地省略参数本身:

int main()

There are also the typical implementation-specific tricks, such as GCC's __attribute__((unused)).

还有典型的特定于实现的技巧,例如 GCC 的__attribute__((unused)).

回答by Nikko

To "disable" this warning, the best is to avoid writing the argument, just write the type.

要“禁用”此警告,最好避免编写参数,只需编写类型即可。

void function( int, int )
{
}

or if you prefer, comment it out:

或者,如果您愿意,请将其注释掉:

void function( int /*a*/, int /*b*/ )
{
}

You can mix named and unnamed arguments:

您可以混合命名和未命名参数:

void function( int a, int /*b*/ )
{
}

With C++17you have [[maybe_unused]] attribute specifier, like:

使用C++17,您有 [[maybe_unused]] 属性说明符,例如:

void function( [[maybe_unused]] int a, [[maybe_unused]] int b )
{
}

回答by Mats Petersson

Macros may not be ideal, but they do a good job for this particular purpose. I'd say stick to using the macro.

宏可能并不理想,但它们在此特定目的方面做得很好。我会说坚持使用宏。

回答by jcayzac

What do you have against the old and standard way?

你有什么反对旧的和标准的方式?

void f(int a, int b)
{
  (void)a;
  (void)b;
  return;
}

回答by Angew is no longer proud of SO

There's nothing new available.

没有什么新东西可用。

What works best for me is to comment out the parameter name in the implementation. That way, you get rid of the warning, but still retain some notion of what the parameter is (since the name is available).

最适合我的是在实现中注释掉参数名称。这样,您就可以摆脱警告,但仍然保留一些关于参数是什么的概念(因为名称可用)。

Your macro (and every other cast-to-void approach) has the downside that you can actually use the parameter after using the macro. This can make code harder to maintain.

您的宏(以及所有其他强制转换为空的方法)的缺点是您可以在使用宏后实际使用该参数。这会使代码更难维护。

回答by manlio

The Boost header <boost/core/ignore_unused.hpp>(Boost >= 1.56) defines, for this purpose, the function template boost::ignore_unused().

<boost/core/ignore_unused.hpp>为此,Boost 头文件(Boost >= 1.56) 定义了函数模板boost::ignore_unused()

int fun(int foo, int bar)
{
  boost::ignore_unused(bar);
#ifdef ENABLE_DEBUG_OUTPUT
  if (foo < bar)
    std::cerr << "warning! foo < bar";
#endif

  return foo + 2;
}


PS C++17 has the [[maybe_unused]]attribute to suppresses warnings on unused entities.

PS C++17 具有[[maybe_unused]]抑制未使用实体的警告的属性。

回答by steeveeet

I really like using macros for this, because it allows you better control when you have different debug builds (e.g. if you want to build with asserts enabled):

我真的很喜欢为此使用宏,因为它可以让您更好地控制何时有不同的调试版本(例如,如果您想在启用断言的情况下进行构建):

#if defined(ENABLE_ASSERTS)
  #define MY_ASSERT(x) assert(x)
#else
  #define MY_ASSERT(x)
#end

#define MY_UNUSED(x)

#if defined(ENABLE_ASSERTS)
  #define MY_USED_FOR_ASSERTS(x) x
#else
  #define MY_USED_FOR_ASSERTS(x) MY_UNUSED(x)
#end

and then use it like:

然后像这样使用它:

int myFunc(int myInt, float MY_USED_FOR_ASSERTS(myFloat), char MY_UNUSED(myChar))
{
  MY_ASSERT(myChar < 12.0f);
  return myInt;
}