I rarely see decltype(auto)
but when I do it confuses me because it seems to do the same thing as auto
when returning from a function.
我很少看到decltype(auto),但当我做它的时候,它会让我感到困惑,因为它在从一个函数返回时似乎也会做同样的事情。
auto g() { return expr; }
decltype(auto) g() { return expr; }
What is the difference between these two syntaxes?
这两个语法有什么不同?
2 个解决方案
#1
40
auto
follows the template argument deduction rules and is always an object type; decltype(auto)
follows the decltype
rules for deducing reference types based on value categories. So if we have
auto遵循模板参数演绎规则,始终是对象类型;decltype(auto)遵循根据值类别推断引用类型的解密规则。如果我们有
int x;
int && f();
then
然后
expression auto decltype(auto)
----------------------------------------
10 int int
x int int
(x) int int &
f() int int &&
#2
9
auto
returns what value-type would be deduced of you assigned the return
clause to an auto
variable. decltype(auto)
returns what type you would get if you wrapped the return clause in decltype
.
自动返回您将返回子句分配给自动变量的值类型。decltype(自动)返回如果你用decltype来包装返回子句,你会得到什么类型。
auto
returns by value, decltype
maybe not.
自动返回值,解密可能不是。
#1
40
auto
follows the template argument deduction rules and is always an object type; decltype(auto)
follows the decltype
rules for deducing reference types based on value categories. So if we have
auto遵循模板参数演绎规则,始终是对象类型;decltype(auto)遵循根据值类别推断引用类型的解密规则。如果我们有
int x;
int && f();
then
然后
expression auto decltype(auto)
----------------------------------------
10 int int
x int int
(x) int int &
f() int int &&
#2
9
auto
returns what value-type would be deduced of you assigned the return
clause to an auto
variable. decltype(auto)
returns what type you would get if you wrapped the return clause in decltype
.
自动返回您将返回子句分配给自动变量的值类型。decltype(自动)返回如果你用decltype来包装返回子句,你会得到什么类型。
auto
returns by value, decltype
maybe not.
自动返回值,解密可能不是。