I have several macros in my code that get as one of the arguments the name of the surrounding class. For example:
struct Foo {
MAKE_NON_COPYABLE(Foo);
// ...
};
(MAKE_NON_COPYABLE basically declares copy ctor, assignment operator, etc. private)
What I want to do is drop the "Foo" argument to the macro. To do this I will need a typedef in the macro which typedefs the surrounding class type. Should look something like this:
#define DECLARE_THIS_TYPE \
typedef /* ? */ ThisType;
#define MAKE_NON_COPYABLE \
DECLARE_THIS_TYPE; \
// declare private ctor, operator= etc. using ThisType
What I tried to do is declare a method whose return type is type of this using decltype:
auto ReturnThisType() -> decltype(*this) { return *this; }
But unfortunately I can't write:
typedef decltype(ReturnThisType()) ThisType;
since I get the following error:
error: cannot call member function ‘Foo& Foo::ReturnThisType()’ without object
And notice I can't use the tricks
typedef decltype(((Foo*)nullptr)->ReturnThisType()) ThisType;
typedef decltype(std::declval<Foo&>().ReturnThisType()) ThisType;
Since the macro doesn't know about Foo...