C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-30-2009, 04:31 AM   #1
Ex scientia vera
 
Join Date: Sep 2007
Posts: 460
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."
IceDane is offline   Reply With Quote
Old 11-30-2009, 04:41 AM   #2
The larch
 
Join Date: May 2006
Posts: 3,175
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
__________________
I might be wrong.

Quote:
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).

Last edited by anon; 11-30-2009 at 04:48 AM.
anon is offline   Reply With Quote
Old 11-30-2009, 04:55 AM   #3
Ex scientia vera
 
Join Date: Sep 2007
Posts: 460
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."
IceDane is offline   Reply With Quote
Old 11-30-2009, 04:59 AM   #4
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,923
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
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is online now   Reply With Quote
Old 11-30-2009, 05:12 AM   #5
The larch
 
Join Date: May 2006
Posts: 3,175
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.

Quote:
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).
anon is offline   Reply With Quote
Old 11-30-2009, 11:33 AM   #6
Senior software engineer
 
brewbuck's Avatar
 
Join Date: Mar 2007
Location: Portland, OR
Posts: 5,664
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?
__________________
"Congratulations on your purchase. To begin using your quantum computer, set the power switch to both off and on simultaneously." -- raftpeople@slashdot
brewbuck is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Structures and dynamically allocated arrays Bandizzle C Programming 7 10-04-2009 02:05 PM
Dynamically allocated array dre C Programming 17 08-13-2009 06:57 PM
dynamically allocated strings?? CS_Student8337 C Programming 18 03-19-2009 05:14 AM
scope of dynamically allocated variables lanzyzhang C Programming 4 07-20-2004 10:59 AM


All times are GMT -6. The time now is 09:36 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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