Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deduce Argument Type For Variadic Template?

Tags:

c++

templates

Is there a way to deduce the type of a list as to avoid having to pass the type of argument the list should be, just deduce it from the first element type.

template<typename T, T... Args>
struct Calc
{
    // do stuff with Args
};

// Usage:
Calc<int, 1, 2, 3>::value;

typename<T... Args> Calc { }; // error T undefined

// Desired:
Calc<1, 2, 3>::value;          // auto deduced T = int
Calc<1.0f, 2.0f, 3.0f>::value; // auto deduced T = float

Is there any way to get the desired behavior or do i have to include the type as a parameter?

like image 387
user3901459 Avatar asked Sep 18 '14 21:09

user3901459


1 Answers

Following the Quentin's suggestion here is an example of a class that allows you to specify the non-type varargs for any type, without spelling the type explicitly. However, floats cannot be used as template non-type arguments.

The following compiles:

#include <type_traits>

enum class Letters {
        Alpha, Beta, Gamma
};

template<auto... Args>
struct Calc;

template<auto Arg1, auto... Args>
struct Calc<Arg1, Args...> {
        using type = decltype(Arg1);
        static type value;
};

template<>
struct Calc<> {
        using type = void;
};

int main() {
        //Calc<1, 2, 3>::value is int
        static_assert(std::is_same_v<typename Calc<1, 2, 3>::type, int>);

        //Calc<Letters::Alpha, Letters::Gamma>::value is Letters
        static_assert(std::is_same_v<typename Calc<Letters::Alpha, Letters::Gamma>::type, Letters>);

        //Calc<>::value does not exist as the type would be `void`
        static_assert(std::is_same_v<typename Calc<>::type, void>);

        return 0;
}
like image 155
CygnusX1 Avatar answered Oct 14 '22 01:10

CygnusX1