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

This is a discussion on std::bitset - Why on earth isn't it dynamically allocated? within the C++ Programming forums, part of the General Programming Boards category; The title of the post sums my question up. I haven't been able to find any information the logic behind ...

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

    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 03: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
    478
    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
    20,968
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,158
    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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21