是否存在“足够”可靠的方法来检测模板参数中的分配器。也就是说,我需要类似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>
。
是否存在“足够”可靠的方法来检测模板参数中的分配器。也就是说,我需要可以在enable_if中使用的is_allocator类型特征之类的东西:假设存在...
标准库中没有这样的is_allocator
特性,但是您可以自己编写:
嗯,在从@TemplateRex和@Casey获得非常有用的答案和评论后,我终于想出了以下[[改进