【发布时间】:2020-06-06 11:11:18
【问题描述】:
给定一个矩阵类
using index_t = int;
template<index_t M, index_t N, typename S>
struct mat {
// matrix implementation
};
我希望有一种通用方法来获取给定类型T 的 elementCount,该方法适用于矩阵和标量。例如,我想能够做到这一点:
dimensionality<mat<1,2,double>>(); // returns 2
dimensionality<mat<2,2,float>>(); // returns 4
dimensionality<double>(); // returns 1
或者可能是这样的:
attributes<mat<1,2,double>>::dimensionality; // returns 2
attributes<mat<2,2,float>>::dimensionality; // returns 4
attributes<double>::dimensionality; // returns 1
我的尝试:
我尝试执行以下操作(认为我部分专注于 struct attributes):
template<typename T>
struct attributes {};
template<typename S, typename = std::enable_if_t<std::is_arithmetic<S>::value>>
struct attributes<S> { // <--- compiler error on this line
static constexpr index_t dimensionality = 1;
};
template<index_t M, index_t N, typename S>
struct attributes<mat<M, N, S>> {
static constexpr index_t dimensionality = M * N;
};
但我在指示的行上收到编译器错误。你能帮助我吗(或者通过提出更好的方法,或者理解我做错了什么)?
【问题讨论】:
标签: c++ templates c++14 template-specialization