C++模板在微控制器编程中的应用
1. 模板默认参数
模板函数和类类型支持默认模板参数。例如,下面定义了一个 point
类模板:
// chapter05_05-001_template_point.cpp
template<typename x_type = std::uint16_t,
typename y_type = x_type>
class point
{
// ...
};
可以通过不同的方式实例化这个模板类:
- point<> pt16_16
:使用默认的 x_type
和 y_type
,即 std::uint16_t
。
point<> pt16_16
{
UINT16_C(1234),
UINT16_C(5678)
};
-
point<std::uint8_t> pt08_08
:指定x_type
为std::uint8_t
,y_type
使用默认值,即std::uint8_t
。