关于c ++:为什么vector< bool> :: reference不返回对bool的引用?

Why vector<bool>::reference doesn't return reference to bool?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#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 );
}

以上示例的编译会产生下一个错误:

1
2
3
4
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&)

我使用G++编译(4.6.1版),如下所示:

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

问题是为什么会这样?vector::reference不是bool&吗?还是编译器的错误?或者,我是在尝试一些愚蠢的事情吗?:)


向量是专业为bool。P></

它是在错误的事情考虑使用instead of the vector:性病。P></

1
2
3
4
5
6
7
8
9
10
11
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...>;

You may need to a occasionally inside the references布尔独立的向量。不幸的是他/她,只给你可以使用vectorto chars references。如果你真的bool&need the Boost库,集装箱检查。非vectorversion of has an en。P></


你的期望是正规,but the is that has been a问题的std::vectorkind of the C++ commitee实验模式。它是真的,在专业商店the bool值模板填充tightly记忆:一位值。P></

和since You cannot have to a位的参考,你有问题。P></


我知道std::vector< bool >packs its contents is stored in each布尔值单位、八位的字节。This is but computationally集约高效存储,处理器必须做arithmetic since the to the requested位访问。和它不工作或与bool参考指针语义,因为位在地址字节在Do not have the C++对象模型中。P></

你可以在我宣布std::vector::referenceof type变量和使用它bool&as if it)。allows to be this GENERIC算法兼容。P></

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

在C + + 11,你可以工作在autoand the using this which automatically selects安&&specifier左值参考向量元素or to the拓展训练拓展训练的参考临时安rvalue。P></

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


std::vector集装箱是不协调的。to optimize航天,EN和packs bools cannot提供参考。P></

使用boost::dynamic_bitsetinstead。P></


只是我的2美分。P></

std::vector::referenceis for which is defined as struct _Bit_referencetypedefP></

1
2
3
4
5
6
7
8
9
10
11
12
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这样的作品,它(嗯,至少没有compiles,T:测试)P></

1
2
3
4
5
6
7
8
9
10
template< typename T >
void callIfToggled( bool v1, std::vector<bool>::reference v2, T & t )
{
    bool b = v2;  
    if ( v1 != b )
    {
        v2 = v1;
        t.foo();
    }
}

编辑:changed the condition from(V1的。T = V2),which不是好主意,(V1的到!B组)。P></


结构与bool在做它,和让vector<>using that the struct型。P></

尝试:P></

sbstruct {boolean b];vectorwhere isP></

然后,你可以说P></

push_back({true})P></

给你P></

然后vector bs;typedef struct sbool {bool b;} boolstruct;P></