Definition of type attributes. Strokes and metaphors

While reading some source code, I found the following definition of signs:

namespace dds { template <typename Topic> struct topic_type_support { }; template <typename Topic> struct topic_data_writer { }; template <typename Topic> struct topic_data_reader { }; template <typename Topic> struct topic_data_seq { }; } #define REGISTER_TOPIC_TRAITS(TOPIC) \ namespace dds { \ template<> struct topic_type_support<TOPIC> { \ typedef TOPIC##TypeSupport type; }; \ template<> struct topic_data_writer<TOPIC> { \ typedef TOPIC##DataWriter type; }; \ template<> struct topic_data_reader<TOPIC> { \ typedef TOPIC##DataReader type; }; \ template<> struct topic_data_seq<TOPIC> { \ typedef TOPIC##Seq type; }; \ } 

It looks weird to me. I would group all the features in a unique class as follows:

 namespace dds { template <typename Topic> struct topic_traits { }; } #define REGISTER_TOPIC_TRAITS(TOPIC) \ namespace dds { \ template<> struct topic_traits<TOPIC> { \ typedef TOPIC##TypeSupport type_support; \ typedef TOPIC##DataWriter data_writter; \ typedef TOPIC##DataReader data_reader; \ typedef TOPIC##Seq seq_type; \ }; \ } 

Can any of you understand why the second approach can be more fragile than the first, or is it much more difficult to add new features?

+7
c ++ templates typetraits
source share
2 answers

Having one class of templates is now called a β€œdash line”. Blob traits are not recommended because they do not work with metafunction (i.e. compile-time functions).

A meta function is a template that takes a class and performs some operation on it. Something like:

 template <class T> class metafunction { typename T::type value = ...; } 

Then you can call the meta function for any of your traits by doing:

 metafunction<topic_type_support<int> >::value; metafunction<topic_data_writer<int> >::value; 

You cannot call a meta function using the blob class because there is now a way to tell the metafund that typedef will use.

If you want to know more about meta functions, I recommend the C ++ Template Metaprogramming book .

+5
source share

This is a matter of style. Your example is probably more convenient to maintain, but the presence of individual types gives the advantage that they are independent - you can easily specialize, say, topic_data_reader for all types of pointers, but leave others unspecialized.

If you want to go deeper, I would question the lack of defaults:

 namespace dds { template <typename Topic> struct topic_traits { typedef typename Topic::type_support type_support; typedef typename Topic::data_writer data_writer; typedef typename Topic::data_reader data_reader; typedef typename Topic::seq_type seq_type; }; } 

This approach means that any class that provides the required typedefs is automatically qualified. The macro can still be used to generate these typedefs or class specializations, but this is probably not necessary ( seq_type , in particular, looks suspicious, as usual, a typedef, not a custom type).

EDIT: With a larger feature class, splitting things up can be used to reduce the number of instances required, but if your feature class contains elements in which using one of them probably means you are using the others, this is not good.

+1
source share

All Articles