【发布时间】:2019-06-18 22:31:50
【问题描述】:
我有几个像这样的类:
struct neg_inf {
constexpr double operator()() { return -std::numeric_limits<double>::infinity(); }
};
struct pos_inf {
constexpr double operator()() { return std::numeric_limits<double>::infinity(); }
};
template<typename dX, class LowerBound, class UpperBound>
class limit {
dX dx;
UpperBound upperBound;
LowerBound lowerBound;
double step_size;
limit( dX x, LowerBound lower, UpperBound upper, double step = 1 ) :
dx{ x }, lowerBound{ lower }, upperBound{ upper }, step_size{ step }
{}
dX value() const { return dx; }
LowerBound lower() const { return lowerBound; }
UpperBound upper() const { return upperBound; }
double step() const { return step_size; }
};
到目前为止,这些类都按预期工作。现在我想使用std::enable_if_t、std::is_arithemticstd::is_same 等条件来修改模板参数。
这些是实例化限制对象需要满足的条件。
dX必须至少为arithmetic和numericalLower&Upperbound必须是numerical、arithmetic或neg_inf或pos_inf。
例如,这些是有效的实例化:
dX = 1st and can be any: int, long, float, double, /*complex*/, etc.
limit< 1st, 1st, 1st > // default template
limit< 1st, 1st, pos_inf > // these will be specializations
limit< 1st, 1st, neg_inf >
limit< 1st, pos_inf, 1st >
limit< 1st, neg_inf, 1st >
limit< 1st, pos_inf, pos_inf >
limit< 1st, neg_inf, neg_inf >
limit< 1st, neg_inf, pos_inf >
limit< 1st, pos_inf, neg_inf >
这些是实例化我的模板的有效条件。当UpperBound 和/或LowerBound 是infinity 类型时,我计划部分专业化该课程。当upper 和lower 边界是数值 - 算术类型时,通用或默认模板将处理它们。
我的问题是 template declaration 与 type_traits 库对我的班级来说会是什么样子?
【问题讨论】:
标签: c++ templates c++17 template-specialization