<Effective Mordern C++>笔记:Item 3:Understand decltype .

这个decltype相对于前面的template和auto就更高级一点了,它不仅可以推断出一个变量的类型,还能推断出一个表达式的类型。

像这样:

const int i=0;
//decltype is const int

bool f(const Class& w);
//decltype is bool(const CLASS&)

if(f(w))
//decltype is bool

vector<type> v;
//decltype v[0] is type& 
//注意operator[]返回一个容器元素的引用
//但是vector<bool>不能返回一个bool&,它返回一个全新的bool对象
在C++11中,新增了尾置返回值的语法,可以利用函数参数推断出返回值类型,而不用担心类型是否在之前已知,常用于template和lambda表达式,C++14中推广到了所有函数
//C++11中的写法
template<typename Container , typename Index>
auto authAndAccess(Container& c,Index i)->decltype(c[i])
{
    authenticateUser();
    return c[i];
}

考虑从以下例子:

template<typename Container , typename Index>
auto authAndAccess(Container& c,Index i)
{
    authenticateUser();
    return c[i];
}

vector<int> v;
authAndAccess(v,5)=10;  // 并不会被编译

因为auto用于函数返回值时,应用的是template的规则,v[5]&的引用属性被忽略,赋值操作失败。
在C++14中,可以允许我们将decltypeauto混用:

template<typename Container , typename Index>
decltype(auto) authAndAccess(Container& c,Index i)
{
    authenticateUser();
    return c[i];
}

decltype(auto)说明符的意思是(auto:返回值类型是被推断出来的;decltype:推断的规则用我的),这样子就能顺利编译啦。
其他情况下,这样的用法也可以被推广:

Widget w;
const Widget& cw=w;
auto myWgt=cw;// type is Widget
decltype(auto) myWgt2=cw;//type is const Widget&

现在又有了新问题:authAndAccess函数有时候需要接受一个临时的容器类型,所以我们需要支持接受一个右值传入,那么我们就可以声明为Container&&使之成为一个universal ref,但是如果&&变量需要传给更深层的函数或移交给其他函数,那么右值性质就可能失效,那么我们可以利用std::forward来实现完美转发,关于std::forward可以参考博文:std::forward-完美转发
最终版本:

template<typename Container , typename Index>
decltype(auto) authAndAccess(Container&& c,Index i)
{
    authenticateUser();
    return std::forward<Container>(c)[i];
}

太完美了…感动的想哭…

最后还有一丢丢要注意的东西,比如我们定义了 int x=0; x是变量名,但是(x)就变成了一个表达式,C++定义(x)也是一个左值,decltype((x))是一个 int&!!!

THE END.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值