How do I terminate variadic template recursion based on the number of elements?

Michael Gazonda

I've written a compile time search and find through template parameters, and it's working fine. I'm stumped about how to go about providing a default value for when there is no item found.

I've tried using sizeof...(args_t) to create a template specialization to terminate on. That isn't allowed. And so, I'm not sure how to go about doing this.

Here's what I've got right now:

template <typename... args_t> class c
{
    template <size_t pos, typename _t, typename... a_t> struct at : at<pos - 1, a_t...> { };
    template <typename _t, typename... a_t> struct at<0, _t, a_t...>
    {
        using t = _t;
    };
};

What I need is something like:

template <typename... args_t> class c
{
    template <> struct at<sizeof...(args_t)>
    {
        using t = default_value;
    };
};

So, how do I go about creating a template specialization based on the number of variadic elements?

Jarod42

You may use the following to manage case where index is outside bound:

template <typename... args_t> class c
{
    template <std::size_t pos, typename... Ts> struct at;

    template <std::size_t pos, typename T, typename... Ts>
    struct at<pos, T, Ts...> : at<pos - 1, Ts...>
    {};

    template <typename T, typename... Ts>
    struct at<0, T, Ts...>
    {
        using type = T;
    };

    template <std::size_t N>
    struct at<N>
    {
        using type = void; // default type
    };

};

Live example

Or using std::tuple and std::conditional (with lazy evaluation):

template <typename... args_t> class c
{
public:
    struct default_type { using type = void; };

    template <std::size_t pos, typename... Ts>
    struct at
    {
        using type =
            typename std::conditional<
                (pos < sizeof...(Ts)),
                std::tuple_element<pos, std::tuple<Ts...>>, // don't use ::type here
                default_type
            >::type::type; // expand type 'twice'.
    };

};

Live example

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I terminate variadic template recursion based on the number of elements?

From Dev

Variadic template recursion, incorrect number of arguments

From Dev

How do I specialise a variadic template class on 0 variadic arguments?

From Dev

How do I enable_if a class with variadic template arguments?

From Dev

How do I access variadic template parameters in a way that is constexpr compatible?

From Dev

How do I access variadic template parameters in a way that is constexpr compatible?

From Dev

How do I count the number of macro arguments passed to a variadic macro?

From Dev

How do I terminate this loop?

From Dev

How do I find a prime number using recursion in Python

From Dev

How do I change required/optional for attributes based on the number of sibling elements?

From Java

How do I get the number of elements in a list?

From Dev

how do I print the number of elements

From Dev

how do I print the number of elements

From Dev

How do you overload a variadic template function?

From Dev

How do I iterate (recurse) backwards through a variadic list of template parameters?

From Dev

In D, how do I specify a variadic template constraint on all the whole tuple?

From Dev

How do I iterate (recurse) backwards through a variadic list of template parameters?

From Dev

How to pass vector elements as arguments to variadic template function?

From Dev

How do I terminate the program in Java?

From Dev

How do I do a SQL query based on number of quantity

From Dev

How do I find the number of template arguments used to declare a class?

From Dev

How do I "enable" a function based on a template parameter?

From Dev

How do I "enable" a function based on a template parameter?

From Dev

WPF How do I add items to a window that was based on a windows template?

From Dev

How do I generate a variadic parameter pack?

From Dev

How do I wrapper a function with variadic parameters

From Dev

How do I generate a variadic parameter pack?

From Dev

How do I number the nodes, if they are sometimes different elements

From Dev

How do I verify the number of elements and content of an array using ParameterMatchers?

Related Related

  1. 1

    How do I terminate variadic template recursion based on the number of elements?

  2. 2

    Variadic template recursion, incorrect number of arguments

  3. 3

    How do I specialise a variadic template class on 0 variadic arguments?

  4. 4

    How do I enable_if a class with variadic template arguments?

  5. 5

    How do I access variadic template parameters in a way that is constexpr compatible?

  6. 6

    How do I access variadic template parameters in a way that is constexpr compatible?

  7. 7

    How do I count the number of macro arguments passed to a variadic macro?

  8. 8

    How do I terminate this loop?

  9. 9

    How do I find a prime number using recursion in Python

  10. 10

    How do I change required/optional for attributes based on the number of sibling elements?

  11. 11

    How do I get the number of elements in a list?

  12. 12

    how do I print the number of elements

  13. 13

    how do I print the number of elements

  14. 14

    How do you overload a variadic template function?

  15. 15

    How do I iterate (recurse) backwards through a variadic list of template parameters?

  16. 16

    In D, how do I specify a variadic template constraint on all the whole tuple?

  17. 17

    How do I iterate (recurse) backwards through a variadic list of template parameters?

  18. 18

    How to pass vector elements as arguments to variadic template function?

  19. 19

    How do I terminate the program in Java?

  20. 20

    How do I do a SQL query based on number of quantity

  21. 21

    How do I find the number of template arguments used to declare a class?

  22. 22

    How do I "enable" a function based on a template parameter?

  23. 23

    How do I "enable" a function based on a template parameter?

  24. 24

    WPF How do I add items to a window that was based on a windows template?

  25. 25

    How do I generate a variadic parameter pack?

  26. 26

    How do I wrapper a function with variadic parameters

  27. 27

    How do I generate a variadic parameter pack?

  28. 28

    How do I number the nodes, if they are sometimes different elements

  29. 29

    How do I verify the number of elements and content of an array using ParameterMatchers?

HotTag

Archive