What does libstdc++'s std::vector<bool>::data do?
Asked Answered
S

1

5

According to the standard, std::vector<bool> has no member function data(). However, the following snippet compiles fine with the latest GCC with libstdc++:

#include <vector>

int main () {
    std::vector<bool> v;
    v.data();
}

If we try to use the result, it turns out the return type is void.

Is this some gcc extension or a bug?
If the former is true, what does it do?

Shoshonean answered 24/10, 2016 at 20:57 Comment(6)
I disagree with the dupe. I am aware that the standard does not define the function in question, I'm asking about the behavior of a specific implementation.Shoshonean
Did you read the source code? GCC is open source.Chlorpromazine
@LightnessRacesinOrbit I admit I did not. Looks like that would have helped. :)Shoshonean
It looks like it also has emplace() and emplace_back(). They're probably no-ops, or something.Accomplished
@JustinTime No, it does need those two.Doubtless
@Doubtless My mistake, I wasn't aware that C++14 added them.Accomplished
M
10

My /usr/include/c++/4.8/bits/stl_bvector.h has:

// _GLIBCXX_RESOLVE_LIB_DEFECTS
// DR 464. Suggestion for new member functions in standard containers.
// N.B. DR 464 says nothing about vector<bool> but we need something
// here due to the way we are implementing DR 464 in the debug-mode
// vector class.
void
data() _GLIBCXX_NOEXCEPT { }

In /usr/include/c++/4.8/debug/vector I see the declaration:

using _Base::data;

So that seems to be the reason: the debug version of std::vector<bool> wouldn't compile unless std::vector<bool>::data existed.

Magnetohydrodynamics answered 24/10, 2016 at 21:6 Comment(2)
Link to the mentioned DR for reference.Shoshonean
Sad. It would be useful if it returned a pointer to the packed buffer. Why not make it private?Ens

© 2022 - 2024 — McMap. All rights reserved.