0

As the subject, many experienced programmers told me "the reference of std::vector is a prvalue"(i.e. std::vector<bool> vec; for(x&: vec) does not compile.). I thought and thought about it, but I still can't understand. I would be very grateful to have some help with this question.

In addition, as per the documentation(https://en.cppreference.com/w/cpp/container/vector_bool/reference), which clearly says that [emphasis mine]:

The std::vector specialization defines std::vector::reference as a publicly-accessible nested class. std::vector::reference proxies the behavior of references to a single bit in std::vector.

The primary use of std::vector::reference is to provide an l-value that can be returned from operator[].

John
  • 2,963
  • 11
  • 33
  • @Nicol Bolas I can't agree with you. Why the document clearly declare the std::vector::reference is to provide an l-value? You see, the URL contains **"vector_bool/reference"**. – John Jun 10 '20 at 05:17
  • 1
    I am not responsible for the contents or specific wording of a Wiki. I don't know what that term is intended to mean. I can only tell you how the C++ language works. – Nicol Bolas Jun 10 '20 at 05:18
  • @user4581301 Thank you. You give a clue. I am a novice in C++11. Why is it a temporary? I can't see such a declaration in the documentation aforementioned indeed. I would appreciate it that if you could tell me how to identify that it's a temporary. – John Jun 10 '20 at 05:25
  • @NicolBolas So, I still wonder why the reference of `std::vector` is a prvalue. How can you identify it? – John Jun 10 '20 at 05:29

0 Answers0