如何通过索引从可变参数模板参数包中提取值?

时间:2014-07-12 06:40:45

标签: c++ templates c++11 variadic-templates compile-time-constant

我想编写一个函数magic_get,它可以通过索引从参数包中提取值,例如:

int n = 0;
n = magic_get<0>(1, 3, 5, 7);
assert(1 == n);
n = magic_get<1>(1, 3, 5, 7);
assert(3 == n);
n = magic_get<2>(1, 3, 5, 7);
assert(5 == n);
n = magic_get<3>(1, 3, 5, 7);
assert(7 == n);

如何实施magic_get

1 个答案:

答案 0 :(得分:14)

template <size_t N, typename... Args>
decltype(auto) magic_get(Args&&... as) noexcept {
    return std::get<N>(std::forward_as_tuple(std::forward<Args>(as)...));
}

如果C ++ 14功能不可用,请将decltype(auto)更改为auto并添加尾随返回类型decltype(/* the whole returned expression here */)


Tupleless版本:

template <std::size_t N, typename Tfirst, typename... Args, std::enable_if_t<N == 0, int>...>
decltype(auto) magic_get(Tfirst&& first, Args&&... as) noexcept {
    return std::forward<Tfirst>(first);
}

template <std::size_t N, typename Tfirst, typename... Args, std::enable_if_t<N != 0, int>...>
decltype(auto) magic_get(Tfirst&& first, Args&&... as) noexcept {
    return magic_get<N - 1>(std::forward<Args>(as)...);
}

请注意,由于clang bug 11723,这在clang中不起作用。将std::enable_if_t<N != 0, int>...替换为std::enable_if_t<N != 0, int> = 0是一种简单的解决方法。