简体   繁体   English

在 C++98 中实现 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.我想知道是否有一种方法可以仅使用 C++98 功能来实现 C++11 运算符decltype

I mean something like我的意思是像

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

Obviously, my code does not compile.显然,我的代码无法编译。

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 .您无法真正实现decltype因为它实际上不是一个函数,而是一个编译器关键字,因为您无法实现,例如returnauto 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.实际上, decltype(...)有点类似于auto ,它被 c++ 预处理器替换为结果类型名,因为与 Haskell 或 Javascript 不同,您不能将类型本身存储在值中。 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;
}
... 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM