1. 程式人生 > >Boost中Core模組的ignore_unused用法

Boost中Core模組的ignore_unused用法

標頭檔案

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()
{}
}