是否有一种“足够”可靠的方法来检测模板参数中的分配器。也就是说,我需要类似is_allocator
类型特征的东西,它可以用于enable_if
:
假设有一个类模板future(带有模板参数T):
// Default ctor with allocator
template <class Alloc, class... Args
class Enable = typename std::enable_if<
is_allocator<Alloc>::value
and std::is_constructible<T, Args...>::value
>::type
>
future(const Alloc& a, Args&&... args)
: _shared_value(std::allocate_shared<T>(a, std::forward<T>(args...))
{
}
// Default ctor (without allocator)
template <class... Args
class Enable = typename std::enable_if<
std::is_constructible<T, Args...>::value
>::type
>
future(Args&&... args)
: _shared_value(std::make_shared<T>(std::forward<T>(args...))
{
}
这里,_shared_value
是一个std::shared_pointer<T>
。