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
- Defined like a struct, I mean field by field (no syntax restrinctions), for example something like this.
- Each field type will be one of this types: int, short, long, byte, bool and std::string.
- And here is the tricky part, I want a tuple where each position will be a reference to each field, in this way I can access to the stored data field by field or get a tuple to it. I have no problem to use std::tuples or boost::tuples.
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