Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>With template metaprogramming and little preprocessing, it is possible to achieve a syntax close to desirable:</p> <pre><code>//one has to "declare" once an attribute name to be able to use //it later in any number of class declarations DECLARE_ATTRIBUTE_NAME(foo); DECLARE_ATTRIBUTE_NAME(quux); DECLARE_ATTRIBUTE_NAME(bar); DECLARE_ATTRIBUTE_NAME(baz); //pass types and declared attribute names, separated by comma typedef TupleWithNamedMembers&lt;int, foo, float, quux, double, bar, char, baz &gt; MyTuple; //extra call to macro "MAKE_TUPLE" can be avoided, see below class MyConstruct: public MAKE_TUPLE(MyTuple) { }; //usage int main(int argc, char* argv[]) { MyConstruct construct; construct.foo = 3; construct.bar = 5.6; construct.quux = 8.9; construct.baz = 'h'; return 0; } </code></pre> <p>The implementation:</p> <pre><code>#ifndef TupleWithNamedMembersH #define TupleWithNamedMembersH //--------------------------------------------------------------------------- #include &lt;Loki/typelist.h&gt; #include &lt;Loki/HierarchyGenerators.h&gt; template&lt;class T, int a&gt; struct attribute { }; //the generated id is not really unique in all cases //one should provide better implementation #define GENERATE_UNIQ_ID(name) ((sizeof(#name)&lt;&lt;16)|__LINE__) //specializations of the struct "attribute" act like compile-time map between //a name ID and an attribute name #define DECLARE_ATTRIBUTE_NAME_IMPL(name, id) \ enum { id = GENERATE_UNIQ_ID(name) }; \ template&lt;class T&gt; \ struct attribute&lt;T,id&gt; \ {\ T name;\ }; #define DECLARE_ATTRIBUTE_NAME(name)\ DECLARE_ATTRIBUTE_NAME_IMPL(name, name) //helps to pass pair of type and name ID as a single type template&lt;class T, int i&gt; struct pair { static const int val = i; typedef T type; }; //unpacks compile-time data from PairT and inherits attribute //with name selected by ID template&lt;class PairT&gt; class holder: public attribute&lt;typename PairT::type,PairT::val&gt; { }; //turns template arguments into Loki::TypeList template &lt; typename T1 = Loki::NullType, int i1 = 0, typename T2 = Loki::NullType, int i2 = 0, typename T3 = Loki::NullType, int i3 = 0, typename T4 = Loki::NullType, int i4 = 0, typename T5 = Loki::NullType, int i5 = 0, typename T6 = Loki::NullType, int i6 = 0, typename T7 = Loki::NullType, int i7 = 0, typename T8 = Loki::NullType, int i8 = 0, typename T9 = Loki::NullType, int i9 = 0, typename T10 = Loki::NullType, int i10 = 0 &gt; struct TupleWithNamedMembers { public: typedef Loki::TL::MakeTypelist&lt;pair&lt;T1,i1&gt;, pair&lt;T2,i2&gt;, pair&lt;T3,i3&gt;, pair&lt;T4,i4&gt;, pair&lt;T5,i5&gt;, pair&lt;T6,i6&gt;, pair&lt;T7,i7&gt;, pair&lt;T8,i8&gt;, pair&lt;T9,i9&gt;, pair&lt;T10,i10&gt; &gt;::Result Result; }; //this macro is required because of internal compiler error that I encounter //Loki::GenScatterHierarchy makes a class inherit every attribute from the type list #define MAKE_TUPLE(types) Loki::GenScatterHierarchy&lt;types::Result, holder&gt; #endif //end of "TupleWithNamedMembers.h" </code></pre> <p>Notes: The MAKE_TUPLE macro should be a metafunction, if your compiler is OK with the code below:</p> <pre><code>template &lt; typename T1 = Loki::NullType, int i1 = 0, typename T2 = Loki::NullType, int i2 = 0, typename T3 = Loki::NullType, int i3 = 0, typename T4 = Loki::NullType, int i4 = 0, typename T5 = Loki::NullType, int i5 = 0, typename T6 = Loki::NullType, int i6 = 0, typename T7 = Loki::NullType, int i7 = 0, typename T8 = Loki::NullType, int i8 = 0, typename T9 = Loki::NullType, int i9 = 0, typename T10 = Loki::NullType, int i10 = 0 &gt; struct MakeTupleWithNamedMembers { private: typedef Loki::TL::MakeTypelist&lt;pair&lt;T1,i1&gt;, pair&lt;T2,i2&gt;, pair&lt;T3,i3&gt;, pair&lt;T4,i4&gt;, pair&lt;T5,i5&gt;, pair&lt;T6,i6&gt;, pair&lt;T7,i7&gt;, pair&lt;T8,i8&gt;, pair&lt;T9,i9&gt;, pair&lt;T10,i10&gt; &gt;::Result type_list; public: typedef Loki::GenScatterHierarchy&lt;type_list, holder&gt; Result; }; //usage class MyConstruct: public MakeTupleWithNamedMembers&lt;int, foo, float, quux&gt;::Result { }; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload