Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Defect report 1207

I don't understand the reason for this defect report 1207, more specifically in reference to the following sentence (emphasis is mine):

Because the transformation of a member name into a class member access expression (9.3.1 [class.mfct.non-static] paragraph 3) only occurs inside the body of a non-static member function, the type of v in the trailing-return-type is non-const but is const in the return expression, resulting in a type mismatch between the return expression and the return type of the function.

Edit

That is, I don't understand why the type of v in the trailing return type is deduced to be non-const.

like image 684
Wake up Brazil Avatar asked Dec 07 '22 00:12

Wake up Brazil


2 Answers

vector v;
auto end() const -> decltype(v.begin()) { return v.begin(); }

decltype(v.begin()), in the trailing-return-type, is iterator - because v has the type vector, seen from outside.

But inside the functions body, the const-specifier of the member function end() is considered. The type of v is the type of this->v - which in turn depends on the const-ness of this.

this' pointee is const (because of the afore-mentioned const-specifier), thus the type of this inside the member function is block const*.

Therefore the type of this->v inside the member function is vector const (because of the const access-path), and v.begin() - which is actually (this->v).begin() - calls the const-overload whose return type is const_iterator. On the other hand, v.begin() in the trailing-return-type "calls" the non-const overload that returns iterator.

That type inconsistence is the problem.

like image 142
Columbo Avatar answered Dec 28 '22 05:12

Columbo


It's saying that, when the trailing-return-type is parsed, the member v is looked up to sufficiently make sense of v.begin() .. but the actual access to v in the context of having invoked end() is not considered, so neither is the fact that end() (and therefore its access to v) is const.

like image 23
Lightness Races in Orbit Avatar answered Dec 28 '22 04:12

Lightness Races in Orbit