roe*_*and 11
当您遇到<bool>专业化时,可能需要升级版本而不是标准版本的情况.
std::vector<bool>实现为bitset,它不将其元素存储为数组bool.
这意味着例如以下代码将不起作用:
template<T>
void handleElement(T &element);
// suppose we get a bool vector:
std::vector<bool> v = ....;
// then this fails because v[i] is a proxy object
handleElement(v[0]);
Run Code Online (Sandbox Code Playgroud)
boost::container::vector<bool> 没有这样的专业化.
我可以编译几个不同之处:
°没有专业化boost::container::vector<bool>(来源@roeland)
decltype(std::vector<bool>(10)[0]) == std::_Bit_reference
decltype(boost::container::vector<bool>(10)[0]) == bool&
Run Code Online (Sandbox Code Playgroud)
°使用Boost分配器基础结构,它(特别是在C++ 1x中)比标准分配器更灵活,不会忽略分配器提供的某些特性.(来源:http://www.boost.org/doc/libs/1_59_0/doc/html/interprocess/allocators_containers.html#interprocess.allocators_containers.containers_explained.stl_container_requirements)
std::vector<double>::allocator_type == std::allocator<double>
boost::container::vector<double>::alloctor_type == boost::container::new_allocator<double>
Run Code Online (Sandbox Code Playgroud)
特别是,仍然可以指定reference和pointer不同的类型T&和T*(参见是否仍然可以自定义STL向量的"引用"类型?)
°支持递归容器(来源:BorisSchäling的Boost C++库).
STL的一些(旧的?)实现不支持不完整的值类型(它们首先不需要),特别是递归容器.
using boost::container::vector;
struct animal{
vector<animal> children; // may not work with std::vector
};
int main(){
animal parent;
animal child1;
animal child2;
parent.children.push_back(child1);
parent.children.push_back(child2);
}
Run Code Online (Sandbox Code Playgroud)
° std::vector是规范而不是实现.boost::container::vector所有平台上只有一个实现,因此可以做出更多假设(例如,最初std::vector不需要使用连续内存)(来源:BorisSchäling的Boost C++库).