Document Number: N3761
Date: 2013-08-29
Author: Sean Middleditch <sean@seanmiddleditch.com>
Project: Programming Language C++, Library Working Group
Reply-To: Sean Middleditch

Proposing type_at<>

Overview

The standard library currently is missing utility templates for extracting types from a variadic template type list, requiring users to instantiate a std::tuple<> in order to extract a type from a specific position within a variadic template type list.

A simple library addition can make certain recurisve uses of variadic templates easier and not dependent on the unrelated concept of a tuple.

This proposal is a pure library addition with no language changes or backwards compatibility regressions. The proposed language for value_at<> depends on N3638, though it could be written without this requirement using type_at<> instead.

Discussion

This paper proposes the new library names type_at and value_at. Some alternative names, should the proposed one be deemed unacceptable, include:

Proposal

In "Header synopsis" in [utility], add:

template <unsigned N, typename T, typename ...R>
struct type_at {
    using type = typename type_at<N - 1, R...>::type;
};

template <typename T, typename ...R>
struct type_at<0, T, R...> {
    using type = T;
};

template <unsigned N, typename T, typename ...R>
auto value_at(T&&, R&&... r) -> decltype(auto) {
    return value_at<N - 1, R...>(std::forward<R>(r)...);
}

template
auto value_at(T&& t, R&&...) -> decltype(auto) {
    return std::forward<T>(t);
}