Question

I have class

struct testdb1 {
    typedef tuple<int, double> value_type;
    static const value_type data[];
};
const testdb1::value_type testdb1::data[] = {
    make_tuple(1, 1.1),
    make_tuple(2, 2.2),
    make_tuple(3, 3.3),
    make_tuple(3, 3.3)

};

Now, I would like to be able to define it in more compact way, I would imagine something like this:

STATIC_DB(
    testdb1,
    make_tuple(1, 1.1),
    make_tuple(2, 2.2),
    make_tuple(3, 3.3),
    make_tuple(3, 3.3)
)

I guess macros are the way to go, but I have no idea how to write (if possible) something like this. If this is possible to do with template, that would be even better, but I don't see how.

Thanks in advance for help

Was it helpful?

Solution

With constexpr, you may do the following (which is compact and clear):

struct testdb1 {
    static constexpr tuple<int, double> data[] =
    {
        make_tuple(1, 1.1),
        make_tuple(2, 2.2),
        make_tuple(3, 3.3),
        make_tuple(3, 3.3)
    };
};

OTHER TIPS

There may have some better way to do this but you can use like this

#define TUPLE(first,second) make_tuple(first,second),
#define TUPLE_LAST(first,second) make_tuple(first,second)
#define STATIC_DB(type,tuples) \
        struct type{\
                typedef tuple<int,double> value_type;\
                static const value_type data[];\
        };\
        const type::value_type type::data[]=\
                tuples;

STATIC_DB(
    testdb1,
    {
        TUPLE(1, 1.1)
        TUPLE(2, 2.2)
        TUPLE(3, 3.3)
        TUPLE_LAST(3, 3.3)
    }
)

Formatted Output:

struct testdb1{ 
    typedef tuple<int,double> value_type; 
    static const value_type data[]; 
}; 

const testdb1::value_type testdb1::data[]= { 
      make_tuple(1,1.1), 
      make_tuple(2,2.2), 
      make_tuple(3,3.3), 
      make_tuple(3,3.3) 
};

may you can try this one:

#define STATIC_DB(t1_, t2_, T_, ...)                                    \
    struct T_ {                                                         \
        typedef tuple<t1_, t2_> value_type;                             \
        static const value_type data[];                                 \
    };                                                                  \
    const T_::value_type T_::data[]   = {                               \
        __VA_ARGS__                                                     \
    };

STATIC_DB(int, double, testDB, make_tuple(1, 1.1), make_tuple(2, 2.2) );
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top