0

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...

alexyav
  • 97
  • 1
  • 5
  • 2
    Related: [Can I implement an autonomous `self` member type in C++?](https://stackoverflow.com/questions/21143835/can-i-implement-an-autonomous-self-member-type-in-c) Your attempt is actually covered in [Konrad Rudolph's answer](http://stackoverflow.com/a/21143918/1508519) –  Feb 11 '14 at 06:32
  • 1
    Ah, I _knew_ there'd be a practical use for this somewhere! – Lightness Races in Orbit Feb 11 '14 at 11:50
  • 1
    Since the proposed answer involves using a base-class anyway and that your goal seems to be about making your type non-copyable, you could consider a solution such as boost::noncopyable. – François Moisan Feb 11 '14 at 18:57

0 Answers0