【问题标题】:Can I have partial template specialization with the using keyword?我可以使用 using 关键字进行部分模板专业化吗?
【发布时间】:2020-01-21 09:38:32
【问题描述】:

OpenCV 库的代码中,我发现了这个:

template<typename _Tp, int cn> class Vec
{
  ...
}

typedef Vec<int, 2> Vec2i;
typedef Vec<int, 3> Vec3i;
typedef Vec<float, 2> Vec2f;
typedef Vec<float, 3> Vec3f;

我想要一个Vec 类型为float 和可变长度N。是否可以写类似的东西

using Vecf<N> = Vec<float, N>;

?

【问题讨论】:

    标签: c++11 templates using


    【解决方案1】:

    是的,你快到了。 正确的语法是

    template<int N>
    using Vecf = Vec<float, N>;
    

    【讨论】:

    • 这意味着,如果我使用Vec&lt;N&gt; 作为一个类中的成员,那么这个类也需要被模板化——它会一直向上传播?
    • 是的,模板在编译时需要所有信息,因此您需要某种方式来提供它。您可能会对这个网站感兴趣type_alias
    猜你喜欢
    • 1970-01-01
    • 2018-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-12
    • 2023-03-24
    • 1970-01-01
    相关资源
    最近更新 更多