Home > other >  C 11 Template Class with Multiple Definitions
C 11 Template Class with Multiple Definitions

Time:01-04

In summary, I would like to have a templated class that can either have a class member that is a std::tuple or an integral type.

The essence of what I want to do is pasted below.

    #include <tuple>
    #include <vector>
    #include <string>

    template<typename T>
    class DATA 
    {
        public:
        
            T value;
        
    };

    template<typename... T>
    class DATA 
    {
        public:
        
            std::tuple<T...> value;
        
    };

    int main(int argc, char *argv[])
    {               
        DATA<int> d1;
        
        d1.value = 10;
        
        DATA<int, std::string, std::vector<int>> d2;
        
        std::get<0>( d2.value ) = 100;
        std::get<1>( d2.value ) = "Hello World";
        std::get<2>( d2.value ).push_back(1);
        std::get<2>( d2.value ).push_back(2);
        std::get<2>( d2.value ).push_back(3);

        return 0;        
    }

CodePudding user response:

C 14 and newer:

template <typename T, typename ...P>
struct A
{
    std::conditional_t<sizeof...(P) == 0, T, std::tuple<T, P...>> value;
};

C 11:

template <typename T, typename ...P>
struct A
{
    typename std::conditional<sizeof...(P) == 0, T, std::tuple<T, P...>>::type value;
};
  • Related