Const decltype(*std::begin(container))& val doesn't make val const?

It seems the compiler tries to apply the const to the int& making it int& const which is superfluous as a reference can't be reseated anyways 1) Try putting the const between the decltype and the reference: decltype(*ints.begin()) const& 1) Thanks for the comments for the clarification Scrap that, thanks to @Ben's comment I noticed the real problem. Try decltype(*ints.cbegin()) cbegin returns a const_iterator which dereferences to a reference-to-const. Also, no need for the extra ampersand, as ints.cbegin() already returns a int const& To explain what went wrong in the OP's code, it's just as @Ben Voigt says in the comments: decltype(*std::begin(ints)) resolves to int& since std::begin(ints) returns a non-const iterator for non-const containers and dereferencing such an iterator returns a reference-to-non-const.

It seems the compiler tries to apply the const to the int&, making it int& const, which is superfluous as a reference can't be reseated anyways1). Try putting the const between the decltype and the reference: decltype(*ints.begin()) const& 1) Thanks for the comments for the clarification. Scrap that, thanks to @Ben's comment I noticed the real problem.

Try decltype(*ints.cbegin()). Cbegin returns a const_iterator, which dereferences to a reference-to-const. Also, no need for the extra ampersand, as *ints.cbegin() already returns a int const&.

To explain what went wrong in the OP's code, it's just as @Ben Voigt says in the comments: decltype(*std::begin(ints)) resolves to int&, since std::begin(ints) returns a non-const iterator for non-const containers and dereferencing such an iterator returns a reference-to-non-const.

GCC should simply ignore the const, making the resulting type an int&. – Johannes Schaub - litb Nov 17 at 20:11 @Johannes: Why? I expect the code to give me a compilation error.

– Viktor Sehr Nov 17 at 22:42 Applying const to int& is perfectly legal: ideone. Com/aLTJO (and Comeau accepts it also). – Ben Voigt Nov 17 at 22:51 @Ben: Interesting, I'll amend my answer accordingly.

Though, just because two compilers accept it doesn't mean it's legal per-se. I'll just believe you and Johannes though. – Xeo Nov 17 at 22:53 1 @Xeo: Your fix won't help.

The declspec part resolves to int&, already a reference. – Ben Voigt Nov 17 at 23:02.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions