3

Is it possible to get the class type

struct A{

DEFINE_CLASS_NAME();

};

such that there is a type A::Type which is A again. Restrictions the macro DEFINE_CLASS_NAME should not have ANY Argument. This is a hypothetical question, if it is possible to access the type in class scope with declval and decltype somehow...

Barry
  • 286,269
  • 29
  • 621
  • 977
Gabriel
  • 8,990
  • 6
  • 57
  • 101
  • 2
    What's wrong with just using `A`? What's the problem you're trying to solve? – Barry Jan 30 '17 at 13:28
  • 1
    Possible duplicate of [Why is there an injected class name?](http://stackoverflow.com/questions/25549652/why-is-there-an-injected-class-name) – Richard Critten Jan 30 '17 at 13:30
  • 1
    Good question. I'm seeing a lot of `invalid use of 'this' outside of a non-static member function` when trying the obvious options. – Jon Chesterfield Jan 30 '17 at 13:39
  • 1
    That's quite the puzzle, but I wonder what would be a practical use case... Even in generic code, you always have *some* way of getting the type of what you're handling from the outside. – Quentin Jan 30 '17 at 13:59
  • 1
    Haven't tested this on all platforms yet and I am not sure about its stability but this might work: https://godbolt.org/g/XfWyiG – Simon Kraemer Jan 30 '17 at 14:15
  • 2
    I don't see what's wrong with `struct A { auto get_this() -> std::remove_pointer_t; typedef std::result_of_t Type; };` but compilers reject `decltype(get_this)`. – Potatoswatter Jan 30 '17 at 14:20
  • @SimonKraemer GCC is afoul of [expr.prim.this]/2: "expression `this`… between the optional *cv-qualifer-seq* and the end… shall not appear within the declaration of a static member function." – Potatoswatter Jan 30 '17 at 14:31
  • @SimonKraemer: Clang complains (rightly) `error: 'this' cannot be used in a static member function declaration` – AndyG Jan 30 '17 at 15:27
  • @Potatoswatter: Isn't that because the return type of `get_this` is not known until after the class declaration is complete? – AndyG Jan 30 '17 at 15:28
  • 1
    @AndyG That's true of deduced return types (C++14 feature), not of trailing return types (C++11 feature). – Potatoswatter Jan 30 '17 at 15:35
  • Related: http://stackoverflow.com/questions/1666802/is-there-a-class-macro-in-c I admit I was hoping for some boost magic – AndyG Jan 30 '17 at 16:27
  • MSVC defines the ``__super`` macro which works in class scope. – Gabriel Feb 01 '17 at 19:19

0 Answers0