[英]What is difference between decltype(auto) and decltype(returning expr) as return type?
[英]What is the difference between auto and decltype(auto) when returning from a function?
我很少看到decltype(auto)
但是当我这样做时会让我感到困惑,因为它似乎与从函数返回时的auto
一样。
auto g() { return expr; }
decltype(auto) g() { return expr; }
这两种语法有什么区别?
auto
遵循模板参数推导规则,并且始终是对象类型; decltype(auto)
遵循decltype
规则,用于根据值类别推断引用类型。 所以,如果我们有
int x;
int && f();
然后
expression auto decltype(auto)
----------------------------------------
10 int int
x int int
(x) int int &
f() int int &&
auto
返回您为auto
变量分配return
子句所推导出的值类型。 decltype(auto)
返回在decltype
包装return子句时将获得的类型。
按值auto
返回, decltype
可能不是。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.