Get the type of the containing class

I want to have a macro that uses the type of the class being used without passing that name to the macro. I tried it for this typedef decltype(*this) my_type;

, but this

can only be used in non-static member functions. Any ideas?

EDIT (copy of comments):

I created a base class and a set of macros that easily implement triple buffering of class data using CRTP. A complication arises when a ternary buffer class inherits from another triple buffer class, effectively having two bases - the CRTP base hidden in the macro and the explicit base. Because the explicit base also inherits from another instance of the CRTP base, the member functions in a derived class conflict between the two bases. I am writing a macro to automatically resolve this re-implementation conflict in a derived class. This reimplementation requires the derived class type to access the correct instance of the CRTP class, hence the original question.

+3


source to share


1 answer


WARNING . Below is a non-standard match. It only works if I pipe -fpermissive

to GCC and it prints an ugly warning when compiled:

#include <type_traits>

#define JOIN( A, B ) JOIN_INTERNAL( A, B )
#define JOIN_INTERNAL( A, B ) A##B
#define UNIQUE_NAME JOIN( unique_prefix_to_typedef_this_, __LINE__ )

template< typename T > struct class_type;
template< typename T, typename C > struct class_type< T C::* > { typedef C type; };

#define TYPEDEF_THIS void UNIQUE_NAME(); typedef class_type< decltype( &UNIQUE_NAME ) >::type

struct A
{
  TYPEDEF_THIS my_type;
  static_assert( std::is_same< my_type, A >::value, "my_type is not A if this fails" );
};

int main()
{
}

      



I hope you can use it, otherwise I'm sure there is no standard suitable solution.

+1


source







All Articles