简体   繁体   English

为什么boost :: dynamic_bitset是模板?

[英]Why is boost::dynamic_bitset a template?

I have used boost::dynamic_bitset before as boost::dynamic_bitset<> , without really thinking about why it is templated. 我之前使用boost::dynamic_bitset作为boost::dynamic_bitset<> ,而没有真正考虑为什么要对其进行模板化。

Though I can understand why std::bitset is templated (the template type is used to specify the size of the bitset), I have now encountered some code of the form boost::dynamic_bitset<unsigned char> and I can't figure out what's the point of the template type. 尽管我可以理解为什么std::bitset是模板化的(模板类型用于指定位集的大小),但是我现在遇到了一些形式为boost::dynamic_bitset<unsigned char> ,我无法弄清楚模板类型的意义是什么?

How is boost::dynamic_bitset<unsigned char> different from boost::dynamic_bitset<> ? boost::dynamic_bitset<unsigned char>boost::dynamic_bitset<>有何不同? Should one be used over the other in any situation? 在任何情况下都应使用另一种方法吗?

From the documentation : 文档中

template <typename Block, typename Allocator>
class dynamic_bitset { // ...

The most obvious advantage of dynamic_bitset being a template is that you can specify your own Allocator type. dynamic_bitset作为模板的最明显的优点是,您可以指定自己的Allocator类型。 This can be useful for a plethora of reasons (performance, memory contiguity, debugging) . 出于多种原因(性能,内存连续性,调试),这可能很有用。 dynamic_bitset does allocate through Allocator if its internal storage needs to grow to accommodate more bits. 如果dynamic_bitset的内部存储需要增长以容纳更多位,则可以通过Allocator分配。

Additionally, it allows you to specify a Block type, which is the underlying primitive used to represent a bunch of bits. 此外,它允许您指定一个Block类型,这是用于表示一堆比特的基础原语。 You might want to change the block type depending on the platform you're on or depending on how much memory you're willing to use (eg a smaller Block type would result in less wasted memory if not all bits are significant) . 您可能要根据所使用的平台或愿意使用的内存量来更改块类型(例如,如果不是所有位都有效,较小的Block类型将导致更少的内存浪​​费)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM