Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing decltype in C++98

Tags:

c++

decltype

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.

like image 964
Paolo M Avatar asked Dec 03 '25 15:12

Paolo M


1 Answers

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;
}
... 
like image 197
Igor Gunin Avatar answered Dec 05 '25 08:12

Igor Gunin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!