Naming tuple elements

I am developing some tuple structure, and I would like to allow the user to use its elements as fields,

DEVELOPMENT:

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
};

At the moment, the user can have it as follows:

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

and use it like

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

It's a little trickier ... So I did it like that.

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 that he can use it directly: myInt () = 0;

My goal is that he could use the tuple as if he had int, bool, stringdata elements without storing the link, which means I need a function (or functor) to get the link, so my solution is good, but the user needs to define the functions. (And the getter looks a lot worse in real code)

So, I would like something like this:

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

   MyFunctor<0> myInt;   //or an alias to a function

   MyFunctor<1> myChar;

   MyFunctor<2> myString;
};
+4
2

, MyFunctor<0> myInt;, , t , , . , , , t ( ).

#define LINK_MEMBER(ID, NAME) decltype(t.get_t<ID>()) NAME() {  \
  return t.get_t<ID>();                                         \
}

struct UserStruct{
   myTuple<int,bool,string> t;
   LINK_MEMBER(0, myInt);   //or an alias to a function
   LINK_MEMBER(1, myChar);
   LINK_MEMBER(2, myString);
};
+2

myInt(), my<int>()? :

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

    template <size_t I>
    decltype(auto) my() { return std::get<I>(data); }

    template <typename T>
    decltype(auto) my() { return std::get<T>(data); }
};

using UserStruct = myTuple<int, bool, std::string>;

, .., .

0

All Articles