One*_*Day 4 c++ templates c++14
C++ 11为函数中的返回类型引入了箭头符号(不知道名称):
template <typename T>
auto fun(T&& a) -> decltype(bar(a)){ ... }
Run Code Online (Sandbox Code Playgroud)
但是根据scott meyer的说法,使用auto作为返回类型本身将删除所有const和引用限定符(因为它遵循与模板推导相同的模式),因此惯用的方法是decltype(auto)将所有限定符保持在类型之上.
但是,在这种背景下,auto推断是decltype(bar(a))?那会decltype(auto)是decltype(decltype(bar(a)))吗?这会多余吗?
假设int& bar();(或使用尾随返回类型语法auto bar() -> int&;),
你可以声明几个函数:
int& f1();或auto f1() -> int&;。decltype(bar()) f2();或auto f2() -> decltype(bar());。(返回类型为int&)。decltype允许 SFINAE 用于模板函数。decltype(auto) f3() { return bar(); }需要定义(推断为int&)(无 SFINAE)。auto f4() { return bar(); }需要定义(推断为int)(无 SFINAE)。decltype(expression)是一个类型,并且decltype(type)是无效的,所以
decltype(decltype(expression))也是无效的。
| 归档时间: |
|
| 查看次数: |
726 次 |
| 最近记录: |