c++decltype

Implementing decltype in C++98


I was wondering if there is a way for implementing the C++11 operator decltype using just C++98 features.

I mean something like

template<class Func>
struct decl_type{
    typedef Func() type;
};

Obviously, my code does not compile.


Solution

  • You can't really implement the decltype since it is not really a function, but a compiler keyword, as you can't implement, for example, return or auto. Actually, decltype(...), somewhat similarly to auto, is replaced with the result typename by the c++ preprocessor, because, unlike, for example, Haskell or Javascript, you cannot have a type itself stored in a value. But you can overload a function multiple times, so you have something like

    void declt(int a) {
        typedef int type;
    }
    void declt(char a) {
        typedef char type;
    }
    ...