Boost中Core模块的ignore_unused用法

本文详细介绍Boost库中的ignore_unused函数,该函数用于消除C++编译过程中未使用变量产生的警告。文章通过多个实例展示了如何在不同场景下应用此函数,包括普通变量及typedef变量的忽略。

头文件

boost/core/ignore_unused.hpp

作用

C++中,没有使用的变量,在编译期间,会出现警告,ingnore_unused就是解决这一问题的。

举例

#include <boost/core/ignore_unused.hpp>

BOOST_CXX14_CONSTEXPR int test_fun(int a)
{
    boost::ignore_unused(a);
    return 0;
}

int main()
{
    {
        int a;
        boost::ignore_unused(a);
    }
    {
        int a, b;
        boost::ignore_unused(a, b);
    }
    {
        int a, b, c;
        boost::ignore_unused(a, b, c);
    }
    {
        int a, b, c, d;
        boost::ignore_unused(a, b, c, d);
    }
    {
        int a, b, c, d, e;
        boost::ignore_unused(a, b, c, d, e);
    }

    {
        typedef int a;
        boost::ignore_unused<a>();
    }
    {
        typedef int a;
        typedef int b;
        boost::ignore_unused<a, b>();
    }
    {
        typedef int a;
        typedef int b;
        typedef int c;
        boost::ignore_unused<a, b, c>();
    }
    {
        typedef int a;
        typedef int b;
        typedef int c;
        typedef int d;
        boost::ignore_unused<a, b, c, d>();
    }
    {
        typedef int a;
        typedef int b;
        typedef int c;
        typedef int d;
        typedef int e;
        boost::ignore_unused<a, b, c, d, e>();
    }

    {
        BOOST_CXX14_CONSTEXPR const int a = test_fun(0);
        boost::ignore_unused(a);
    }

    return 0;
}

源代码

namespace boost {
template <typename... Ts>
BOOST_FORCEINLINE BOOST_CXX14_CONSTEXPR void ignore_unused(Ts const& ...)
{}

template <typename... Ts>
BOOST_FORCEINLINE BOOST_CXX14_CONSTEXPR void ignore_unused()
{}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值