Thread: std::bitset - Why on earth isn't it dynamically allocated?

  1. #1
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477

    std::bitset - Why on earth isn't it dynamically allocated?

    The title of the post sums my question up. I haven't been able to find any information the logic behind this anywhere - there is a lot of people bashing it, however.

    Do you guys have any idea why they chose to implement it as such? I am by no means an experiences C++ programmer, and I knew that it was possible to use templates to pass arguments like that, but am I the only one who thinks it's a perversion of the whole concept of generic programming? For some reason, I don't think it makes sense to be able to pass templates anything but types.

    I know boost has a bitset that does it dynamically, but I'm still curious as to the logic behind this design. It really limits its usage.
    "What's up, Doc?"
    "'Up' is a relative concept. It has no intrinsic value."

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    It is much more efficient this way?

    You could similarly bash std::tr1::array for not allocating the array dynamically. It's not supposed to.

    If that is not what you need, use something else, like boost's dynamic_bitset.

    Besides, non-type template arguments are used heavily in C++ (e.g think boost and type_traits, half of which are instantiations of integer_constant<bool, true/false> or something like that. Neither should it particularly interfere with generic programming:

    Code:
    template <class Bitset>
    void foo(const Bitset& bs); //accepts all bitsets and anything else with a similar interface
    
    template <unsigned N>
    void foo(const std::bitset<N>& bs); //accepts bitsets of any size
    Last edited by anon; 11-30-2009 at 04:48 AM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    Quote Originally Posted by anon View Post
    It is much more efficient this way?

    You could similarly bash std::tr1::array for not allocating the array dynamically. It's not supposed to.

    If that is not what you need, use something else, like boost's dynamic_bitset.

    Besides, non-type template arguments are used heavily in C++ (e.g think boost and type_traits, half of which are instantiations of integer_constant<bool, true/false> or something like that. Neither should it particularly interfere with generic programming:

    Code:
    template <class Bitset>
    void foo(const Bitset& bs); //accepts all bitsets and anything else with a similar interface
    
    template <unsigned N>
    void foo(const std::bitset<N>& bs); //accepts bitsets of any size
    Why would it be much more efficient? Wouldn't the only overhead by the single allocation of memory when it instantiated?

    I did mention that boost has a dynamic bitset, but I still don't understand why the functionality wasn't provided from the get-go.
    "What's up, Doc?"
    "'Up' is a relative concept. It has no intrinsic value."

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by IceDane
    I know boost has a bitset that does it dynamically, but I'm still curious as to the logic behind this design. It really limits its usage.
    One possibility is that due to design by committee, they ended up going with something that they could agree on, despite its limitations. Also, if I understand the possible implementations correctly, if you only need a fixed sized bitset, std::bitset is likely to provide more efficient operations than boost::dynamic_bitset.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Dynamic allocations may be very expensive in C++. (If you see a benchmark where OMG Java is much faster than C++, the culprit seems to be quite often that the benchmark in reality tests the speed of small object allocation-deallocation.)

    Besides, there is a dynamic bitset in standard C++, and that's the vector<bool> specialization (it is deprecated, though, since it doesn't work like a container).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by IceDane View Post
    Do you guys have any idea why they chose to implement it as such?
    What would distinguish it from std::vector<bool> then?
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structures and dynamically allocated arrays
    By Bandizzle in forum C Programming
    Replies: 7
    Last Post: 10-04-2009, 02:05 PM
  2. Dynamically allocated array
    By dre in forum C Programming
    Replies: 17
    Last Post: 08-13-2009, 06:57 PM
  3. dynamically allocated strings??
    By CS_Student8337 in forum C Programming
    Replies: 18
    Last Post: 03-19-2009, 05:14 AM
  4. scope of dynamically allocated variables
    By lanzyzhang in forum C Programming
    Replies: 4
    Last Post: 07-20-2004, 10:59 AM