Why vector::reference doesn't return ref

2019-01-01 11:30发布

#include <vector>

struct A
{
    void foo(){}
};

template< typename T >
void callIfToggled( bool v1, bool &v2, T & t )
{
    if ( v1 != v2 )
    {
        v2 = v1;
        t.foo();
    }
}

int main()
{
    std::vector< bool > v= { false, true, false };

    const bool f = false;
    A a;

    callIfToggled( f, v[0], a );
    callIfToggled( f, v[1], a );
    callIfToggled( f, v[2], a );
}

The compilation of the example above produces next error :

dk2.cpp: In function 'int main()':
dk2.cpp:29:28: error: no matching function for call to 'callIfToggled(const bool&, std::vector<bool>::reference, A&)'
dk2.cpp:29:28: note: candidate is:
dk2.cpp:13:6: note: template<class T> void callIfToggled(bool, bool&, T&)

I compiled using g++ (version 4.6.1) like this :

g++ -O3 -std=c++0x -Wall -Wextra -pedantic dk2.cpp

The question is why this happens? Is vector<bool>::reference not bool&? Or is it a compiler's bug?
Or, am I trying something stupid? :)

6条回答
残风、尘缘若梦
2楼-- · 2019-01-01 11:33

Vector is specialized for bool.

It is considered a mistake of the std. Use vector<char> instead:

template<typename t>
struct foo {
  using type = t;
};
template<>
struct foo<bool> {
  using type = char;
};

template<typename t, typename... p>
using fixed_vector = std::vector<typename foo<t>::type, p...>;

Occasionally you may need references to a bool contained inside the vector. Unfortunately, using vector<char> can only give you references to chars. If you really need bool&, check out the Boost Containers library. It has an unspecialized version of vector<bool>.

查看更多
人气声优
3楼-- · 2019-01-01 11:34

std::vector<bool> is a non conforming container. To optimize space, it packs bools and cannot provide reference.

Use boost::dynamic_bitset instead.

查看更多
泛滥B
4楼-- · 2019-01-01 11:35

Your expectations are normal, but the problem is that std::vector<bool> has been a kind of experiment by the C++ commitee. It is actually a template specialization that stores the bool values tightly packed in memory: one bit per value.

And since you cannot have a reference to a bit, there's your problem.

查看更多
十年一品温如言
5楼-- · 2019-01-01 11:43

Just my 2 cents:

std::vector<bool>::reference is a typedef for struct _Bit_reference which is defined as

typedef unsigned long _Bit_type;

struct _Bit_reference
  {
    _Bit_type * _M_p;
    _Bit_type _M_mask;

    // constructors, operators, etc...

    operator bool() const
    { return !!(*_M_p & _M_mask); }
  };

Changing the function like this, it works (well, compiles at least, haven't tested):

template< typename T >
void callIfToggled( bool v1, std::vector<bool>::reference v2, T & t )
{
    bool b = v2;  
    if ( v1 != b )
    {
        v2 = v1;
        t.foo();
    }
}

EDIT: I changed the condition from (v1 != v2), which wasn't a good idea, to (v1 != b).

查看更多
路过你的时光
6楼-- · 2019-01-01 11:47

Make a structure with a bool in it, and make the vector<> using that struct type.

Try:

vector<struct sb> where sb is struct {boolean b];

then you can say

push_back({true})

do

typedef struct sbool {bool b;} boolstruct; and then vector<boolstruct> bs;

查看更多
只若初见
7楼-- · 2019-01-01 11:50

std::vector< bool > packs its contents so each Boolean value is stored in one bit, eight bits to a byte. This is memory-efficient but computationally intensive, since the processor must perform arithmetic to access the requested bit. And it doesn't work with bool reference or pointer semantics, since bits within a byte do not have addresses in the C++ object model.

You can still declare a variable of type std::vector<bool>::reference and use it as if it were bool&. This allows generic algorithms to be compatible.

std::vector< bool > bitvec( 22 );
std::vector< bool >::reference third = bitvec[ 2 ];
third = true; // assign value to referenced bit

In C++11, you can work around this using auto and the && specifier which automatically selects an lvalue reference bound to the vector element or an rvalue reference bound to a temporary.

std::vector< bool > bitvec( 22 );
auto &&third = bitvec[ 2 ]; // obtain a std::vector< bool >::reference
third = true; // assign value to referenced bit
查看更多
登录 后发表回答