c++ - 部分特化类型特征时如何使用 std::decay?

标签 c++ templates c++11 typetraits partial-specialization

我创建这些类型特征是为了确定类型是否是动态容器,但最近在对 vector 的引用未返回 true 时遇到了困惑。

template<typename T>
struct is_dynamic_container
{
    static const bool value = false;
};

template<typename T , typename Alloc>
struct is_dynamic_container<std::vector<T , Alloc>>
{
    static const bool value = true;
};

我想我需要使用 std::decay ,但我无法确定是否可以像这样而不是在调用站点完成。

template<typename T , typename Alloc>
struct is_dynamic_container<std::decay<std::vector<T , Alloc>>::type>
{
    static const bool value = true;
};

^^这行不通。

我只想能够写is_dynamic_container<std::vector<int>&>而不是 is_dynamic_container<std::decay<std::vector<int>&>::type> .这可能吗?

最佳答案

template<class T>
using is_dynamic_container_with_decay = is_dynamic_container<std::decay_t<T>>;

关于c++ - 部分特化类型特征时如何使用 std::decay?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29587258/

相关文章:

c++ - 以模板化容器作为参数的模板函数 - 奇怪的拼写错误

c++ - 我什么时候应该选择复制省略而不是通过 const 引用传递参数?

c++ - 有没有办法提取模板的参数列表以放入另一个元函数?

c++ - 通过快速用户切换检测 Mac OS X 中事件 session 的 API

c++ - unordered_map 桶的节点大小

c++ - 创建模板函数以使用该函数参数调用其他函数

codeigniter - 使用codeigniter中的模板发送电子邮件

c++ - 媒体基础视频重新编码产生音频流同步偏移

c++ - QT 中的 INCLUDEPATH 不起作用

c++ - 成员类实例化