【发布时间】:2014-10-08 08:25:55
【问题描述】:
template<typename T>
struct foo{
void f(){
decltype(*this) a(*this);
do_some_test(a);
}
T data;
};
//compiler won't accept this
在我的解释中,decltype 应该返回 a 类型,以便我们可以在声明中使用它。但是google说在decltype(x)中,如果x是左值,它将返回T&,其中T是x的类型。
但是他们设计它来返回参考是为了什么?此外,我应该怎么做才能在模板中创建与*this具有相同类型的类的实例?
【问题讨论】:
-
还有这个问题是关于如何用 decltype Using new with decltype创建一个新实例
-
“但是他们设计它来返回引用的目的是什么?” 每个表达式都有一个值类别(左值、xvalue 或 prvalue)。
decltype通过应用引用限定符来反映该属性:左值表达式的左值引用限定符、xvalues 的右值引用和纯右值的无引用。 -
是的,link 应该是一个答案。为重复道歉。
标签: c++ templates generic-programming