寻找用于`enable_if`的`is_allocator`类型特性>>

问题描述 投票:5回答:3

是否存在“足够”可靠的方法来检测模板参数中的分配器。也就是说,我需要类似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_valuestd::shared_pointer<T>

是否存在“足够”可靠的方法来检测模板参数中的分配器。也就是说,我需要可以在enable_if中使用的is_allocator类型特征之类的东西:假设存在...

templates c++11 template-meta-programming sfinae enable-if
3个回答
5
投票

标准库中没有这样的is_allocator特性,但是您可以自己编写:


1
投票

嗯,在从@TemplateRex和@Casey获得非常有用的答案和评论后,我终于想出了以下[[改进


© www.soinside.com 2019 - 2024. All rights reserved.