Как вытащить конкретный аргумент из variadic template
От: Аноним  
Дата: 27.08.13 13:35
Оценка:
Я правильно понимаю, что единственный способ обратиться к конкретному аргументу из variadic template (учитывая, что все они могут быть разных типов) — это сделать что-то наподобие такого

#include <iostream>
#include <tuple>

template <typename... Args>
void foo(Args... args)
{
    std::tuple<Args...> temp = std::make_tuple(args...);
    std::cout << std::get<0>(temp) << '\n';
}

int main()
{
    foo(0, 1.0f, "str");
}


или такого

#include <iostream>

template <typename A1, typename... Args>
A1 get_first(const A1& first_arg, Args... other_args)
{
    return first_arg;
}

template <typename A1, typename A2, typename... Args>
A2 get_second(const A1& first_arg, const A2& second_arg, Args... other_args)
{
    return second_arg;
}

template <typename A1, typename A2, typename A3, typename... Args>
A3 get_third(const A1& first_arg, const A2& second_arg, const A3& third_arg, Args... other_args)
{
    return third_arg;
}

// etc

template <typename... Args>
void some_function_receiving_var_args(Args... args)
{
    std::cout << get_first(args...) << '\n'
              << get_second(args...) << '\n'
              << get_third(args...) << '\n';
}

int main()
{
    some_function_receiving_var_args(0, "str", 1.5);
}


Или есть более интересные способы?
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.