0

I am trying to create a template code which gives the functionality to create vector of n different types and then insert data in the vector of the particular type associated with it.

#include <iostream>
#include <tuple>
#include <vector>

template <typename T>
struct is_same {
    using type = T;
};

template <typename T>
struct UnderlyingTypeInSymlist {
    using type = std::vector<T>;
};

template <template <typename> class ToActual_ = is_same, typename... Types>
class TupleOfTypes {
  private:
    struct dummy_type {};
  public:
    template <typename T>
    using ToActual = typename ToActual_<T>::type;
  protected:
    static std::tuple<ToActual<Types>...> tuple_;
  public:
    using UnderlyingType = decltype(tuple_);
    static constexpr std::size_t TypeCount = std::tuple_size<UnderlyingType>::value;

    template <typename T, std::size_t I>
    ToActual<T> Get() {
        return std::get<I> (tuple_);
    }
};


int main () {
    using Parent = TupleOfTypes<UnderlyingTypeInSymlist, int, char, float, double, short, long>;
    Parent vect_;
    // auto& list = Parent::template Get<int, (std::size_t)0>();
    auto& list = vect_.template Get<int, (std::size_t)0>();
    for (int i=0; i<50; i++) {
        list.emplace_back(i);
    }
    for (auto a : list) {
        std::cout<<a<<std::endl;
    }
    return 0;
}

It should print the numbers starting form 0 to 49 b ut it is giving some compilation error.

prog.cpp: In function 'int main()':
prog.cpp:36:57: error: invalid initialization of non-const reference of type 'std::vector<int>&' from an rvalue of type 'TupleOfTypes<UnderlyingTypeInSymlist, int, char, float, double, short int, long int>::ToActual<int> {aka std::vector<int>}'
     auto& list = vect_.template Get<int, (std::size_t)0>();
                                                         ^

What is the error with the above code. Also some clarification about any wrong section of the code would be helpful.

Piyush Vijay
  • 165
  • 9

0 Answers0