【发布时间】:2019-08-14 01:34:34
【问题描述】:
cppreference page for the Allocator requirement 没有说 Allocator 必须是可继承的,即它没有说 Allocator 不能是最终的。
但是,在许多库中,分配器是私有继承的,以利用无状态分配器的空基类优化。例如:
template <typename T, typename A = std::allocator<T>>
class Dummy_vector :private A {
// ...
A get_alloc() const
{
return static_cast<A>(*this);
}
// ...
};
如果A 是最终的,则此实现会中断。
分配器可以是最终的吗?我错过了什么?或者这样的实现是否应该包含用于最终 Allocators 的特殊代码?
(注意:“最终分配器s的特殊代码,”我的意思是这样的:
template <
typename T,
typename A = std::allocator<T>,
bool = std::is_final<A>
>
class Dummy_vector :private A {
// version for nonfinal allocators
// ...
A get_alloc() const
{
return static_cast<A>(*this);
}
// ...
};
template <typename T, typename A>
class Dummy_vector<T, A, true> {
// special version for final allocators
};
)
【问题讨论】:
标签: c++ inheritance memory-management final allocator