美文网首页
C++ 遍历可变模板参数 iterate variadic te

C++ 遍历可变模板参数 iterate variadic te

作者: Raven7 | 来源:发表于2017-06-22 08:45 被阅读0次

template<size_t I = 0, typename FuncT, typename ...Tp>
inline typename std::enable_if_t<I == sizeof ...(Tp)> for_each(std::tuple&, FuncT)
{
}

template<size_t I = 0, typename FuncT, typename ...Tp>
inline typename std::enable_if_t<I < sizeof ...(Tp)> for_each(std::tuple<Tp ...>& t, FuncT f)
{
    f(std::get(t));   
    for_each<I + 1, FuncT, Tp...>(t, f);
}

template<typename ...Args>
auto print(Args ...args)
{
    auto a = std::forward_as_tuple(args...);
    for_each(a, [](auto x)
    {
        std::cout << typeid(x).name() << ":" << x << std::endl;
    });
}

int main()
{
    print(10, 'c', "bbb", string("123"));
}

结果如下:

int:10

char:c

char const *:bbb

class std::basic_string, class std::allocator >:123

相关文章

网友评论

      本文标题:C++ 遍历可变模板参数 iterate variadic te

      本文链接:https://www.haomeiwen.com/subject/oqmxcxtx.html