我想知道是否有一种方法可以仅使用C ++ 98功能来实现C ++ 11运算符decltype
。
我的意思是
template<class Func>
struct decl_type{
typedef Func() type;
};
显然,我的代码无法编译。
您不能真正实现 decltype
,因为它实际上不是函数,而是编译器关键字,因为您无法实现,例如,return
或auto
。实际上,decltype(...)
与auto
有点类似,但由c ++预处理器替换为结果类型名称,因为与例如Haskell或Javascript不同,您不能将类型本身存储在值中。但是您可以多次重载一个函数,因此您有类似以下内容:
void declt(int a) {
typedef int type;
}
void declt(char a) {
typedef char type;
}
...