0

Say I have the following code:

std::vector<std::string> foo({"alice", "bob"});
for (const std::string &f : foo)
    std::cout << f.size() << std::endl;

if I make a mistake and change f.size() to f->size(), I get the following error from GCC:

error: base operand of ‘->’ has non-pointer type ‘const string {aka const std::basic_string}

Why is the actual type const std::basic_string<char> rather than const std::basic_string<char> & (reference)?

5
  • What leads you to think it is not const std::basic_string<char> &? Commented Aug 13, 2013 at 19:33
  • the somewhat cryptic compiler message probably.. Commented Aug 13, 2013 at 19:35
  • What does this have to do with iterators? Because f isn't an iterator. Commented Aug 13, 2013 at 19:48
  • @NicolBolas quite right, I should have said, "Type of iterated element...". Commented Aug 13, 2013 at 21:08
  • @tmatth: Then change your question to say that. Commented Aug 14, 2013 at 0:54

1 Answer 1

1

The left-hand side of -> operator is an expression. There ain't no such thing as an expression with a reference type:

5p5 If an expression initially has the type “reference to T” (8.3.2, 8.5.3), the type is adjusted to T prior to any further analysis. The expression designates the object or function denoted by the reference, and the expression is an lvalue or an xvalue, depending on the expression.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.