What happens when you call data() on a std::vector<bool>?
Asked Answered
O

4

42

C++11 has implemented data() member function on std::vector, which gives you a pointer to the memory array. Does this mean the template specialization std::vector<bool> have this member as well? Since this specialization doesn't store the data in terms of bool *, what kind of behavior can you expect from calling data() ?

Ovoid answered 15/5, 2013 at 15:20 Comment(0)
C
29

This page documenting the class explicitely indicates that the specialization does not provide this method.

The specialization has the same member functions as the unspecialized vector, except data, emplace, and emplace_back, that are not present in this specialization.

This other page as well as §23.3.7 of the C++ specifications do confirm it.

Columbian answered 15/5, 2013 at 15:25 Comment(11)
C++11 23.3.7 indicates the same thing, which may be of more interest ;)Californium
@Yakk: Is there something wrong with that page, or are you just following the unexplained hatred I keep seeing for that site around here?Aestival
@dionadar I just checked the specs and found the relevant section, thanks for the hint though.Columbian
@Yakk there's cppreference.com if you prefer, but it doesn't explicitely state that this method is not defined, though it lists the defined members.Columbian
@MikeSeymour: The "hate" may have something to do with questionable information like the while (myfile.good()) example on this page. Novice programmers are using this bad practice as I type this.Evanescent
@Blastfurnace: Fair point; and I hope all the people being redirected to cppreference instead don't follow this example of unlocking a lock that you might not own. The moral: never trust the internet.Aestival
@Columbian The problem with citing an unreliable source isn't that the unreliable source is always wrong, but rather that it is weak evidence that what you are saying is true. And if you cite it as if it was reliable, people might think that whatever else they read from that source is reliable.Right
@Yakk if the tutorials are not up to a given standard, the reference documentation provided seems pretty much conforming to the specs afaik. Maybe you found something there that contradicts my opinion?Columbian
@Columbian Yep, I've seen incorrect stuff on there. As an example off the top of my head, stream string operator overloads where "close, but not quite". And I don't have much use for an unreliable reference site, so I avoid using it.Right
@Yakk, well, I included the cppreference link up there, but I didn't feel like quoting the whole specialization declaration, so I'll leave it like that. However, I appreciate your concern and will keep your advice in mind. The specs should be enough in most cases. Thanks!Columbian
@MikeSeymour cppreference is an open wiki, and relies on the readers to contribute fixes and content. Just bringing up a problem on the talk page would attract attention.Powys
A
34

It won't compile, unless your implementation has a non-standard extension. The specialisation of std::vector<bool>, as specified in C++11 23.3.7/1, doesn't declare a data member.

Aestival answered 15/5, 2013 at 15:22 Comment(2)
I tried to do this, and the gcc error that I get is that data is a "void value not ignored as it should be", which implies that the function is implemented, but has a void return type. Which is curious.Wiener
@MatsPetersson: Curious indeed. GCC defines a void function, with a mysterious comment saying "we need something here due to the way we are implementing DR 464 in the debug-mode vector class" (DR 464 is the proposal to add data() to the generic vector template).Aestival
C
29

This page documenting the class explicitely indicates that the specialization does not provide this method.

The specialization has the same member functions as the unspecialized vector, except data, emplace, and emplace_back, that are not present in this specialization.

This other page as well as §23.3.7 of the C++ specifications do confirm it.

Columbian answered 15/5, 2013 at 15:25 Comment(11)
C++11 23.3.7 indicates the same thing, which may be of more interest ;)Californium
@Yakk: Is there something wrong with that page, or are you just following the unexplained hatred I keep seeing for that site around here?Aestival
@dionadar I just checked the specs and found the relevant section, thanks for the hint though.Columbian
@Yakk there's cppreference.com if you prefer, but it doesn't explicitely state that this method is not defined, though it lists the defined members.Columbian
@MikeSeymour: The "hate" may have something to do with questionable information like the while (myfile.good()) example on this page. Novice programmers are using this bad practice as I type this.Evanescent
@Blastfurnace: Fair point; and I hope all the people being redirected to cppreference instead don't follow this example of unlocking a lock that you might not own. The moral: never trust the internet.Aestival
@Columbian The problem with citing an unreliable source isn't that the unreliable source is always wrong, but rather that it is weak evidence that what you are saying is true. And if you cite it as if it was reliable, people might think that whatever else they read from that source is reliable.Right
@Yakk if the tutorials are not up to a given standard, the reference documentation provided seems pretty much conforming to the specs afaik. Maybe you found something there that contradicts my opinion?Columbian
@Columbian Yep, I've seen incorrect stuff on there. As an example off the top of my head, stream string operator overloads where "close, but not quite". And I don't have much use for an unreliable reference site, so I avoid using it.Right
@Yakk, well, I included the cppreference link up there, but I didn't feel like quoting the whole specialization declaration, so I'll leave it like that. However, I appreciate your concern and will keep your advice in mind. The specs should be enough in most cases. Thanks!Columbian
@MikeSeymour cppreference is an open wiki, and relies on the readers to contribute fixes and content. Just bringing up a problem on the talk page would attract attention.Powys
A
4

No. Per std::vector<bool>

Does not necessarily store its data in a single contiguous chunk of memory.

There is no data() member.

Appellative answered 15/5, 2013 at 15:24 Comment(0)
S
1

Well, there is no std::vector<bool>::data, so what you can expect is a compile error.

Starobin answered 15/5, 2013 at 15:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.