从函数返回时,auto和decltype(auto)有什么区别? [英] What is the difference between auto and decltype(auto) when returning from a function?

查看:224
本文介绍了从函数返回时,auto和decltype(auto)有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很少看到 decltype(auto),但是当我这样做时,我感到困惑,因为它似乎和 auto

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.

auto g() { return expr; }
decltype(auto) g() { return expr; }

这两种语法有什么区别?

What is the difference between these two syntaxes?

推荐答案

auto 遵循模板参数推导规则,并且始终是对象类型; decltype(auto)遵循 decltype 规则,用于基于值类别推导引用类型。因此,如果我们有

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

int x;
int && f();

然后

expression    auto       decltype(auto)
----------------------------------------
10            int        int
x             int        int
(x)           int        int &
f()           int        int &&

这篇关于从函数返回时,auto和decltype(auto)有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆