is there a (clean) way to prevent the use of the
vector<bool> specialization in the STL? With the
specialization, the following does not work as
operator[] returns a struct __bit_reference instead
of a bool& .
#include <vector>
vector<bool> bv(10);
bool& first_element(bv[0]);
Regards
--
Ingolf Koch http://www.minet.uni-jena.de/~ingolf/
FSU Jena, Institut fuer Angewandte Mathematik, 07740 Jena
[ Send an empty e-mail to c++-...@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
You can specialize it for your own Allocator type.
Implement it with
struct YourAlloc : public std::allocator {};
template <>
class std::vector<bool,YourAlloc>
{
union Bool { bool bit; char dummy; };
vector<Bool,YourAlloc> vec_;
bool& operator [] (size_t i) { return vec_[i].bit; }
// etc.
};
Or just give it a different name. The containers in the
standard are just examples, after all.
Nathan Myers
n...@nospam.cantrip.org http://www.cantrip.org/
This means that I have to recode std::vector which is not
exactly what I am looking for.
What I am searching for is a way to "tell" STL to not
use the specialized version but the default vector
template.
Regards
--
Ingolf Koch http://www.minet.uni-jena.de/~ingolf/
FSU Jena, Institut fuer Angewandte Mathematik, 07740 Jena
[ Send an empty e-mail to c++-...@netlab.cs.rpi.edu for info ]
: In article <6a51nh$h...@netlab.cs.rpi.edu>, n...@nospam.cantrip.org
: (Nathan Myers) writes:
: |> Ingolf Koch <ing...@maxp09.mathe.uni-jena.de> wrote:
: |> >is there a (clean) way to prevent the use of the
: |> >vector<bool> specialization in the STL?
: |>
: |> struct YourAlloc : public std::allocator {};
: |>
: |> template <>
: |> class std::vector<bool,YourAlloc>
: |> {
: |> union Bool { bool bit; char dummy; };
: |> vector<Bool,YourAlloc> vec_;
: |> bool& operator [] (size_t i) { return vec_[i].bit; }
: |> // etc.
: |> };
: |>
: |> Or just give it a different name. The containers in the
: |> standard are just examples, after all.
: This means that I have to recode std::vector which is not
: exactly what I am looking for.
Understood. This discussion went around before with lots of
complaints about the specialization.
: What I am searching for is a way to "tell" STL to not
: use the specialized version but the default vector
: template.
And from the original post.
: #include <vector>
: vector<bool> bv(10);
: bool& first_element(bv[0]);
I really dislike references/pointers/iterators to members of any of
the standard containers, but especially vector/dequeue since there are
almost no assurances about their validity.
vector<bool> is a space saving specialization. Saving space is a
sometimes desirable thing. The library provides it. However, it is
not a time saver.
Since this thing seems to be objected to by so many, maybe, it should
have been placed in a sub_standard (sorry I can't give credit to the
original coiner) namespace like the relational templates. But it
wasn't.
How about:
typedef int MyBool;
vector<MyBool> bv(10);
With all of the standard conversions between bool and int, this might
just be doable. Please do not complain about space since there is no
requirement that sizeof(bool) <= sizeof(int).
Waiting for holes to be poked in this hackery.
John
>is there a (clean) way to prevent the use of the
>vector<bool> specialization in the STL?
I don't know how clean this is but (untested code):
class unpacked_bool
{
bool imp;
public:
unpacked_bool(bool b = false) : imp(b) { }
unpacked_bool(int b) : imp(b) { }
unpacked_bool(const unpacked_bool& b) : imp(b.imp) { }
operator const bool&() const { return imp; }
operator bool&() { return imp; }
// Sorry, operator int() causes overloading ambiguities
bool& operator=(bool b) { return imp = b; }
bool& operator=(int b) { return imp = b; }
bool& operator=(const unpacked_bool& b) { return imp = b.imp; }
};
vector<unpacked_bool> boolvec;
-------------------------------------------------------------
Pablo Halpern phal...@truffle.ultranet.com
I am self-employed. Therefore, my opinions *do* represent
those of my employer.