类成员参数中的Constexpr
Constexpr in class member parameters
我想在类声明的各个部分的std::array的模板参数中指定一个变量,比如so:
class SetAngles
{
public:
constexpr int txSize() const { return 19; }
constexpr int rxSize() const { return ack.size(); }
void txParse(std::array<uint8_t, txSize()>& packet)
{
...
}
private:
std::array<uint8_t, txSize()> txPacket = {0xFA, 0x0E, 0x11, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
};
因此,如果我需要更改常量,我不必在不同的地方多次更改它。显然,上面不是constexpr的工作方式。。。我还尝试过使用公共成员变量而不是函数,但仍然失败。我得到编译器错误:
cannot call member function ‘constexpr int GimbalPacket::SetAngles::txSize() const’ without object
我知道#define语句可以工作,但我希望将其封装在类中。有什么建议吗?
我没能像最初想要的那样在类中使用constexpr。最终,我只在类外放置了一个constexpr,并将两者封装在命名空间中以完成封装。
相关文章:
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- Constexpr 可变参数模板,用于对无符号整数进行重新排序
- MSVC 和函数参数的 constexpr?
- 使用constexpr + auto作为返回和参数类型的奇怪类型推导
- constexpr 函数的常量引用参数:gcc/msvc vs clang/icc
- 从非类型模板参数声明 constexpr 数组的可移植方法
- 为什么带有指针子对象的文字类类型的 constexpr 表达式不能是非类型模板参数
- 调用模板参数 constexpr 方法?
- constexpr 运算符重载使用参数的问题
- 使用模板参数还包括 constexpr 成员函数enable_if单独定义和声明模板成员函数
- 当迭代器(输入参数)通常不是constexpr时,constexpr算法真的有用吗
- 类成员参数中的Constexpr
- MSVC使用constexpr-if从可变模板方法中的基本模板参数中吞下const
- 在if constexpr中使用带参数包的概念时,升级到gcc 9后出现编译错误
- 在 constexpr-if 条件下比较 constexpr 函数参数会导致错误
- 不接受静态成员函数作为 constexpr 参数
- ODR-使用转发的 constexpr 参数
- 为什么不允许使用'constexpr'参数?
- 如果直接使用 constexpr-function,则认为 constexpr 参数,但如果用于调用另一个 conste
- 代码中奇怪的constexpr参数