-1

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

Community
  • 1
  • 1
Trungus
  • 165
  • 8
  • 3
    No idea what you're asking. Can you provide a sample usage of such a thing? – Barry Aug 03 '15 at 17:55
  • 1
    I have to agree with Barry: it is unclear what you're asking here. Perhaps providing a (even non-working) minimal sample would make things more understandable. – Marco A. Aug 03 '15 at 17:58

1 Answers1

0

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.

R2-Dequeue
  • 638
  • 1
  • 5
  • 15
  • Hi R2, your example is clear and fully functional, but in my case I can't use it, because I need to declare a lot of class of this kind, so is a lot of work to do. – Trungus Aug 04 '15 at 16:56