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()
?
Asked
Active
Viewed 5,879 times
41

Narut Sereewattanawoot
- 864
- 6
- 14
4 Answers
33
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.

Mike Seymour
- 249,747
- 28
- 448
- 644
-
7I 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. – Mats Petersson May 15 '13 at 15:33
-
3@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). – Mike Seymour May 15 '13 at 15:42
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.

didierc
- 14,572
- 3
- 32
- 52
-
6C++11 23.3.7 indicates the same thing, which may be of more interest ;) – danielschemmel May 15 '13 at 15:36
-
5@Yakk: Is there something wrong with that page, or are you just following the unexplained hatred I keep seeing for that site around here? – Mike Seymour May 15 '13 at 15:44
-
@dionadar I just checked the specs and found the relevant section, thanks for the hint though. – didierc May 15 '13 at 15:47
-
@Yakk there's [cppreference.com](http://en.cppreference.com/w/cpp/container/vector_bool) if you prefer, but it doesn't explicitely state that this method is not defined, though it lists the defined members. – didierc May 15 '13 at 15:59
-
3@MikeSeymour: The "hate" may have something to do with questionable information like the `while (myfile.good())` example on [this page](http://www.cplusplus.com/doc/tutorial/files/). Novice programmers are [using this bad practice](http://stackoverflow.com/questions/16569651/eof-bad-good-functions-arent-detected-by-autocomplete) as I type this. – Blastfurnace May 15 '13 at 16:01
-
2@Blastfurnace: Fair point; and I hope all the people being redirected to cppreference instead don't follow [this example](http://en.cppreference.com/w/cpp/thread/mutex/try_lock) of unlocking a lock that you might not own. The moral: never trust the internet. – Mike Seymour May 15 '13 at 16:08
-
@didierc 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. – Yakk - Adam Nevraumont May 15 '13 at 17:54
-
@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? – didierc May 15 '13 at 21:33
-
1@didierc 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. – Yakk - Adam Nevraumont May 15 '13 at 21:40
-
@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! – didierc May 15 '13 at 21:46
-
2@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. – Cubbi May 18 '13 at 22:42
4
No. Per std::vector<bool>
Does not necessarily store its data in a single contiguous chunk of memory.
There is no data()
member.

masoud
- 55,379
- 16
- 141
- 208
1
Well, there is no std::vector<bool>::data
, so what you can expect is a compile error.

PlasmaHH
- 15,673
- 5
- 44
- 57