服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - c++ decltype关键字的用法

c++ decltype关键字的用法

2021-09-29 13:17半杯茶的小酒杯 C/C++

这篇文章主要介绍了c++ decltype关键字的用法,帮助大家更好的理解和学习c++,感兴趣的朋友可以了解下

1. decltype关键字的用途是什么

给定变量的名称或者表达式,decltype返回变量或者表达式的类型。如下所示:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
const int i = 0; // decltype(i) is const int
 
bool f(const Widget& w); // decltype(w) is const Widget&,decltype(f) is bool(const Widget&)
 
struct Point {
 
int x, y; // decltype(Point::x) is int, decltype(Point::y) is int
 
};
 
Widget w; // decltype(w) is Widget
 
if (f(w)) ... // decltype(f(w)) is bool
 
template<typename T>class vector {
 
public:
 
...
 
T& operator[](std::size_t index);...
 
};
 
vector<int> v; // decltype(v) is vector<int>
 
if (v[0] == 0) ... // decltype(v[0]) is int&

2.decltype主要应用场景是模板函数

decltype在实际的开发中主要用于模板函数中,函数的返回值依赖于模板参数类型的情况。如下authAndAccess函数的返回值类型依赖于Container的元素类型。

?
1
2
3
4
5
6
7
template<typename Container, typename Index>
auto authAndAccess(Container& c, Index i) -> decltype(c[i]) {
 
 authenticateUser();
 
 return c[i];
}

此处的返回值auto并非类型推导的意思,而是C++ 11中的函数返回类型后置的表达方式,表明函数的返回类型在参数列表之后。函数返回类型后置的优势在于我们可以用函数的参数来指定返回值。

在c++ 14中auto关键字可以独立用于对函数的返回值进行类型推导,而不必采用c++ 11中的返回返回类型后置的声明方式:

?
1
2
3
4
5
6
7
template<typename Container, typename Index>
auto authAndAccess(Container& c, Index i) {
 
 authenticateUser();
 
 return c[i]; // return type deduced from c[i]
}

但上述写法在实际应用针对具体case可能存在问题,比如如果operator[]返回T&,auto的推导机制会返回T,下面的就会编译失败:

?
1
2
3
4
5
std::deque<int> d;
 
...
 
authAndAccess(d, 5) = 10; //return d[5], then assign 10 to it; this won't compile!

因为根据auto的推导机制,authAndAccess返回的是右值,所以编译不通过。authAndAccess函数需要声明为如下方式才可以保证该示例编译通过。

?
1
2
3
4
5
6
7
template<typename Container, typename Index>
decltype(auto) authAndAccess(Container& c, Index i){
 
 authenticateUser();
 
 return c[i];
}

decltype(auto)不仅仅可以用于函数,也可以用于变量,可以完美推导变量的类型。

?
1
2
3
4
5
Widget w;
const Widget& cw = w;
auto myWidget1 = cw; // auto type deduction: myWidget1's type is Widget
 
decltype(auto) myWidget2 = cw; // decltype type deduction: myWidget2's type is const Widget&

再回到authAndAccess函数

?
1
2
template<typename Container, typename Index>
decltype(auto) authAndAccess(Container& c, Index i);

注意到Container是一个非const的左值引用,这意味着用户可以修改Container内元素的值,同时也意味不能传递右值引用给它。

另外右值容器一般是一个临时对象,会在函数调用结束后不久被销毁,所以当用户传入一个右值引用的时候,一般我们要把返回元素拷贝它的一个副本。如何能够在不重载authAndAccess函数的情况下使它同时支持左值和右值呢?答案是通用引用。

?
1
2
template<typename Container, typename Index>
decltype(auto) authAndAccess(Container&& c,Index i);

为了保证推导结果的正确性,需要在实现中增加完美转发(std::forward)功能。

?
1
2
3
4
5
6
7
template<typename Container, typename Index>
decltype(auto)authAndAccess(Container&& c, Index i){
 
 authenticateUser();
 
 return std::forward<Container>(c)[i];
} // c++ 14版本
?
1
2
3
4
5
6
7
8
template<typename Container, typename Index>
auto authAndAccess(Container&& c, Index i)
-> decltype(std::forward<Container>(c)[i])
{
 authenticateUser();
 
 return std::forward<Container>(c)[i];
} // c++ 11版本

3. decltype使用的极端case

?
1
2
3
4
5
6
7
8
9
10
11
decltype(auto) f1() { // decltype(x) is int, so f1 returns int
 int x = 0;
 ...
 return x;
}
 
decltype(auto) f2() { // decltype((x)) is int&, so f2 returns int&
 int x = 0;
 ...
 return (x);
}

返回了一个局部变量的引用。

4. 需要记住的:

1) decltype总是返回与变量或者表达式完全相同的类型;

2) 对于类型T的非名称的左值表达式,decltype总是返回T&;

以上就是c++ decltype关键字的用法的详细内容,更多关于c++ decltype关键字的资料请关注服务器之家其它相关文章!

延伸 · 阅读

精彩推荐