0

For example, I use macro to write a constructor method

#define DEFAULT_CONSTRUCTOR    T() { ...; }

class A
{
    DEFAULT_CONSTRUCTOR;
};

However, I don't know the concrete type T. I try decltype(*this). It cannot be used in constructor. Any way to do it? Please don't use macro arguments.

user1899020
  • 13,167
  • 21
  • 79
  • 154
  • You have to name the type for the constructor. What are you trying to do anyway? – Praetorian Jun 20 '14 at 04:13
  • 1
    Similar, but constructors need the exact name anyway: http://stackoverflow.com/questions/21143835/can-i-implement-an-autonomous-self-member-type-in-c – chris Jun 20 '14 at 04:18
  • I write some macros to implement constructors, assignments, clone and put them together in a macro "DEFAULTS_ALL(members...)" or "DEFAULTS_ALL(T, members...)". Just want to use the 1st version. – user1899020 Jun 20 '14 at 04:19
  • You just have to pass the name of the class to that macro. – JarkkoL Jun 20 '14 at 04:25
  • @user1899020, Why not just an editor snippet that makes all of those while creating the class? – chris Jun 20 '14 at 04:32
  • @chris Macros with arguments seem easy to change. And I use BOOST_PP_FOR_EACH a lot where snippet doesn't seem to help. – user1899020 Jun 20 '14 at 04:37

1 Answers1

0

You should give argument to the macro.

like this:

#define DEFAULT_CONSTRUCTOR(CLS_NAME) CLS_NAME() { ...; }

now in your class: (do you want it to be a private ctor?)

class A
{
    DEFAULT_CONSTRUCTOR(A);
};
SHR
  • 7,940
  • 9
  • 38
  • 57