decltype(* int_iterator + 0)的类型是什么?

时间:2018-08-21 15:58:18

标签: c++

template <typename AIterator>
auto foo(AIterator begin) -> decltype(*begin + 0){
    return *begin;
}

例如:

vector<int> ivec = {1,2,3};
foo(ivec.begin());

我的答案书说它是const reference类型,是真的吗?

但是IIRC,decltype(int reference + int)的表达式类型是int*begin是引用,因此我自然地认为*begin + 0也应导致{{ 1}})。

例如:

int

PS:我在VS上尝试过,IDE提示函数int a = 3, &b = a; decltype(b + 0) d; //d is a int 的返回类型为foo

这本书是 C ++ Primer 5th 的答案书,但是我的书不是英语版本,并且是翻译人员改编的,因此我一开始没有提及。

enter image description here

1 个答案:

答案 0 :(得分:3)

它是int,这正是作者添加+ 0…的快速原因,以摆脱引用类型并确保foo返回副本(出现达到目的)。

类型为decltype always evaluates to T prvalue 表达式上的

T

这本书的散文是错误的,或者您读错了它。