在c ++ 11中的未使用的参数 [英] Unused parameter in c++11

查看:838
本文介绍了在c ++ 11中的未使用的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

例如

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

return 0;
}

但是宏并不是c ++的最佳实践,所以。
有什么更好的解决方案出现与c ++ 11标准?



Edit2: http://stackoverflow.com/users/1463557/madscientist\">MadScientist 建议好的解决方案

  template<类型名T> 
void ignore(T&&)
{}

Xeo 了解多个参数

  template< class ... T> void unused(T&& ...)
{}


解决方案

我为了这个目的使用了一个空体的函数:

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

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



我希望任何严重的编译器优化函数调用,


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

#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?

Thanks for all!

Edit2: MadScientist suggested good solution

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

Solution from Xeo for multiple parameters

template<class... T> void unused(T&&...)
{ }

解决方案

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.

这篇关于在c ++ 11中的未使用的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆