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

≯℡__Kan透↙ 提交于 2019-12-07 04:34:19

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/40227414/what-does-libstdcs-stdvectorbooldata-do

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!