c++ - What is the type of decltype(*int_iterator + 0)?
问题描述
template <typename AIterator>
auto foo(AIterator begin) -> decltype(*begin + 0){
return *begin;
}
For example:
vector<int> ivec = {1,2,3};
foo(ivec.begin());
My answer book says it's a const reference
type, is it true?
But IIRC, decltype(int reference + int
), expression type is a int
(*begin
is a reference, so naturally I think *begin + 0
should also result in a int
).
For example:
int a = 3, &b = a;
decltype(b + 0) d; //d is a int
PS: I tried on VS and the IDE hints the return type of function foo
is int
.
The book is C++ Primer 5th answer book, but mine is not a English version and it's adapted by the translators, so I didn't mention it at first.
解决方案
它是int
, 这正是作者添加+ 0
... 作为摆脱引用类型并确保foo
返回副本的快速方法的原因(这似乎是它的目的)。
decltype
在类型的纯右值表达式上T
总是计算为T
.
要么这本书的散文是错误的,要么你读错了。
推荐阅读
- oracle - 码头 OJDBC java.net.ConnectException:连接超时:连接
- html - 找不到如何制作一个程序,在使用时将特定网站的特定部分打印成 pdf 文件
- javascript - Processing.js - 特定数组的 updatePixels()
- kubernetes - 使用 istio 在应用版本之间进行负载平衡时如何使用子集?
- maven - 更新 Maven 继承属性时的版本更新
- javascript - 正则表达式:中间有一个点的字符串
- .net-core - 如何存储 IEnumerable 的属性
在 Cosmos 表中,使用 EF Core 3.1 - swift - 如何从 UInt8 数组转换为数据?
- python - 也许我从根本上误解了 python 中的缩进?- Python
- java - 无法实例化类错误消息 - 在 testNG 中