vendredi 3 juillet 2015

Naming tuple elements

I am developping a some kind of tuple structure, and I would like to allow the user to use its elements as fields,

EXPLAINING :

this is my tuple :

template<typename ...Ts>
struct myTuple{
   std::tuple<Ts...> data;

   template<size_t I>
   inline type<I>& get_t() {   // type<I> is the I'th type
      return std::get<I>(data);
   }

   // Other stuff
};

For the moment the user can have it this way :

struct UserStruct{
   myTuple<int,bool,string> t;
   // Other stuff
}

and use it like,

UserStruct ob;
ob.t.get_t<0>() = 0;

Which is a little bit complex... So i made it this way

struct UserStruct{
   myTuple<int,bool,string> t;

   decltype(mo.get_t<0>()) myInt() {
      return mo.get_t<0>();
   }

   decltype(t.get_t<1>()) myChar() {
      return t.get_t<1>();
   }

   decltype(t.get_t<2>()) myString() {
      return t.get_t<2>();
   }
};

so he can use it directly : myInt() = 0;

My goal is that he could use the tuple as if he had an int, bool, string data members without storing the references, which means I need a function ( or a functor ) to get the reference, so my solution is good, but it needs the user to define the functions. (And the getter looks much worse in the real code)

So I would like something like this :

struct UserStruct{
   myTuple<int,bool,string> t;

   MyFunctor<0> myInt;

   MyFunctor<1> myChar;

   MyFunctor<2> myString;
};

Aucun commentaire:

Enregistrer un commentaire