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[].

4
  • @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". Commented Jun 10, 2020 at 5: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. Commented Jun 10, 2020 at 5: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. Commented Jun 10, 2020 at 5:25
  • @NicolBolas So, I still wonder why the reference of std::vector<bool> is a prvalue. How can you identify it? Commented Jun 10, 2020 at 5:29

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.