Reputation: 165
In first place, I'm not a very skilled programmer in C++11 and templates, I read a lot of posts but I can't figure out how to write my idea (if is even possible), saying that, this is my idea.
My idea is create a complex compiler-time type, I'm trying to follow this rules
Every suggestion, example, advise and any other comment are welcome, I'm trying to learn how to do this.
Thanks in advance
Best Regards
Edit: I can give you the code where I trying to do this, perhaps is not the best aprouch so I'm open to suggestions.
#include <iostream>
#include <tuple>
/* Trait Def */
template<typename T>
struct field_trait
{
typedef T type;
typedef type &ref;
typedef type *pointer;
typedef const type &const_ref;
};
/* Field Def */
template <typename T>
struct Field : field_trait<Field<T>>
{
typedef typename T::type value_type;
typedef typename Field<T>::type field_type;
typename T::type storage;
typename T::ref &operator[](const T &c)
{
return storage;
};
};
/* Linear inheritance def */
template<class...Ts>
struct operator_index_inherit {};
template<class T0, class T1, class...Ts>
struct operator_index_inherit<T0, T1, Ts...> : T0, operator_index_inherit<T1, Ts...>
{
using T0::operator[];
using operator_index_inherit<T1, Ts...>::operator[];
};
template<class T0>
struct operator_index_inherit<T0>: T0
{
using T0::operator[];
};
template<class... Fields>
struct bind : operator_index_inherit<Field<Fields>...>
{
using base = operator_index_inherit<Field<Fields>...>;
using base::operator[];
bind() : data(make_tuple(int(0),string("")))
{};
typedef std::tuple<typename Field<Fields>::value_type&... > tuple_t;
tuple_t data;
};
/* Data type def */
struct t_age : field_trait<int>{};
struct t_name : field_trait<std::string>{};
typedef Field<t_age> age;
int main()
{
bind<t_age,t_name> data;
data[t_age()] = 123;
data[t_name()] = "pepe";
return 0;
}
This code don't compile, the error is caused by the declaration of type "tuple_t" and "tuple_t data"
Regards
Upvotes: -1
Views: 270
Reputation: 638
Taking a guess at what you're looking for, I've got a quick, crude example:
#include <iostream>
#include <string>
#include <tuple>
class Type
{
public:
Type() : t{std::tie(a, b, c, d, e, f)} {}
int a;
short b;
long c;
unsigned char d;
bool e;
std::string f;
std::tuple<int&, short&, long&, unsigned char&, bool&, std::string&> t;
};
int main()
{
Type A{};
A.c = 5;
std::cout << std::get<2>(A.t) << std::endl;
return 0;
}
With a demo.
Upvotes: 0