ez_lcw 2022-10-02 22:04 采纳率: 100%
浏览 17
已结题

C++ 类模板的简单问题

想实现类似下面的功能:

template<int D>
struct A:vector<A<D-1>>;

using A<0> = int;

即实现一个类似 D 维矩阵的东西。

但是好像没法把 A<0> 特化成 int。不知如何解决?

  • 写回答

2条回答 默认 最新

  • ez_lcw 2022-10-20 07:55
    关注

    已解决,可以这么实现:

    template <int D>
    struct _kdpoly : vector<_kdpoly<D-1>> {};
    
    template <>
    struct _kdpoly<1> : vector<int> {};
    
    template <int D>
    struct _kdpoly_type {
        using type = _kdpoly<D>;
    };
    
    template <>
    struct _kdpoly_type<0> {
        using type = int;
    };
    
    template <int D>
    using kdpoly = typename _kdpoly_type<D>::type;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 10月28日
  • 已采纳回答 10月20日
  • 创建了问题 10月2日