Reputation: 7506
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.
Upvotes: 1
Views: 211
Reputation: 385164
It's int
, and this is exactly why the author has added + 0
… as a quick way to get rid of the reference type and ensure that foo
returns a copy (which appears to be its purpose).
decltype
on a prvalue expression of type T
always evaluates to T
.
Either the book's prose is wrong, or you've misread it.
Upvotes: 3