继承 c++ 中的特征
Inheriting traits in c++
我正在用 c++ 创建一个特征,它将我所做的另一个特征作为模板输入。 但是,当我运行此代码时,我收到以下编译器错误:
错误:模板参数数错误(1,应为 2( 模板<\测量<\int v,单位 u> a>
代码如下:
enum class Unit { km, m, cm };
template<int v, Unit u>
struct Measure
{
public:
static const int value = v;
static const Unit unit = u;
};
template< Measure<int v, Unit u> a>
struct Measure_add
{
public:
static const int value = a::value;
static const Unit unit = a::unit;
};
用法应为:
std::cout << Measure_add< Measure<4, Unit::m> >::value << std::endl;
这应该给出:
四
Measure_add
可以通过以下方式从Measure
继承:
template<class>
struct Measure_add;
template<int v, Unit u>
struct Measure_add<Measure<v, u>> : Measure<v, u> {};
static_assert(Measure_add<Measure<4, Unit::m>>::value == 4);
不确定这是什么意思:
template< Measure<int v, Unit u> a>
您可能想要这个:
template< typename a>
struct Measure_add
{
public:
static const int value = a::value;
static const Unit unit = a::unit;
};
现在您可以通过以下方式实例化它
using m_add = Measure_add< Measure<4,Unit::m> >;
相关文章:
- 如何声明特征矩阵,然后通过嵌套循环初始化它
- 特征::矩阵<双精度,1,3> 结构类型函数中的返回类型函数
- 有没有一种方法可以通过"typedef"为重新定义的基本类型定义特征和强制转换运算符
- 特征命名访问向量段
- 将特征矩阵的向量设置为0
- 特征:模板函数中矩阵的平面图
- basic_string的前导/尾部不区分空格的特征
- 特征 3 类的模板专用化
- 特征 c++:复矩阵的面积双曲正切(atanh)
- C++ 中的特征向量计算
- 根据C++标准的定义实现"is_similar"类型特征
- C++类型特征,以查看是否可以<uint32_t>对类型"K"的任何变量调用"static_cast(k)"
- 有没有办法找到特征矩阵系数的中值?
- 如何将高维数据映射到特征类型?
- 将平面阵列重塑为复杂的特征类型
- 特征 LLT 模块给出不正确的结果?
- 特征模板化函数和维度
- 特征稀疏向量:求最大系数
- 特征 3.3.x:如何在所有行中操作 lamba?
- 如何将向量断言到特征矩阵