Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

When I declare a class static method, is it possible to refer current class using decltype (or in any other similar style)? For example,

class
    static AAA const make();

I am trying to make something like this.

class
    static decltype(*this) const make();  // Not working because there's no `this`.

The *this is used to describe what I want to do. I want to know some decltype() expression which can be resolved to AAA.

If it's possible how can I do that?

I don't know why you want to trade the clear and concise form of static AAA const make() for the indirect static decltype(*this) const make(). Readability is king, and being explicit about your types helps your readers a lot. – cmaster - reinstate monica Nov 25, 2013 at 21:46 Is the idea behind this to add a make function to a whole hierarchy of classes in a particular "smart" way? That'd be a case for the curiously recurring template parameter pattern. If this is only for one class, I don't understand the need to derive the return type, AAA would do fine (but then, I don't understand what's wrong with calling the constructor, either). – Damon Nov 26, 2013 at 0:23 I don't have specific instance of use-case. I suddenly am curious on this, and if this is possible, I think maybe I can figure out some interesting applications later. I don't claim any goodness of badness of idea in this question. I also thought this wouldn't that much useful, but this question was bugging me for a long time, so I decided to ask this. – eonil Nov 26, 2013 at 15:00

This is not legal in C++11 as you need to specify a return type for make(). In C++98/03/11 you can:

class
public:
    typedef AAA Self;
    static Self make()
        return AAA();

That is low-tech, but very readable.

<aside>

You should avoid returning const-qualified types by value. This inhibits efficient move semantics. If you want to avoid assigning to rvalues, then create an assignment operator qualified with &.

</aside>

AAA(int i): _i(i) {} static auto make(int i) -> typename std::remove_reference<decltype(*this)>::type return {i}; void print() std::cout << _i << std::endl; int main() AAA aaa = AAA::make(1); aaa.print(); return 0;

It compiles on GCC 4.7.2 at least :)

EDIT 26/11-13: The above code is not legal c++, even though it compiles with gcc, it does not with clang or icpc. My apologies. User hvd already made a similar suggestion (now deleted), but it's not allowed: [expr.prim.general]/3 about the expression this: "It shall not appear before the optional cv-qualifier-seq and it shall not appear within the declaration of a static member function" – dyp Nov 26, 2013 at 11:34

Just invented a way using member pointers, and it seems to work: https://godbolt.org/z/v-g5z0

#define self_test(name) \ void dummy__##name(void) {} \ template static T type__##name( void (T::*func)(void) ) { return T(); } \ typedef decltype(type__##name(&dummy__##name)) self__##name; \ static void test( void ) { self__##name::print(); } struct T1 { self_test(X); static void print() { printf( "this is T1\n" ); } struct T2 { self_test(X); static void print() { printf( "this is T2\n" ); } int main() { T1::test(); T2::test();

This is not perfect either, it requires -fpermissive to compile with gcc, but at least gcc/VS/Intel all compile it and it works. Also in fact gcc/mingw doesn't even require -fpermissive for this.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.