constexpr类中的C 常量指针
c++ constant pointer in the constexpr class
美好的一天!请帮助我以获取代码,我尝试将ConstexPR类与未来的const指针进行指向非恒定变量,并更改非构造变量,我的编译器说
"error: ‘Actuator{const Pin{1ul, 1ul}, const Pin{1ul, 2ul}, const Pin{1ul, 3ul}, ((velocity_type*)(& velocity))}’ is not a constant expression"
,
对象ACT1始终生命,因为它的手臂嵌入式设备代码
代码:
#include <cstddef>
typedef std::size_t port_type;
typedef std::size_t pin_type;
typedef std::size_t velocity_type;
class Pin {
private:
port_type const _port;
pin_type const _pin;
public:
constexpr Pin(port_type const port, pin_type const pin) :
_port { port }, _pin { pin } {
}
};
class Actuator {
private:
Pin const _enable_pin;
Pin const _dir_pin;
Pin const _step_pin;
velocity_type* const _velocity; //constant pointer to non-constant variable
public:
constexpr Actuator(Pin const ep, Pin const dp, Pin const sp, const velocity_type velocity) :
_enable_pin { ep }, _dir_pin { dp }, _step_pin { sp }, _velocity(const_cast<velocity_type*>(&velocity)) {
}
void set_velocity(const velocity_type velocity) const {*_velocity = velocity;} //try to change velocity
};
int main() {
constexpr Actuator act1 ( Pin { 1, 1 }, Pin { 1, 2 }, Pin { 1, 3 }, 1u );
act1.set_velocity(1u);
}
根据c 标准[expr-const]/2(强调矿山(
表达式E是核心常数表达式,除非 e,遵循抽象机器的规则,将评估之一 以下表达式:
(...(
15. reinterpret_cast
((velocity_type*)(& velocity))
绝对是重新诠释铸造的一种形式,因此不能在恒定表达式中使用...
相关文章:
- 将常量指针引用绑定到非常量指针
- 如何使用数据对象上的常量指针初始化类
- 为什么我收到"从常量指针到指针的转换无效?
- C++/QT:使用指向私有成员的常量指针作为只读数据共享
- 是否可以使用非常量指针调用非常量函数,以及当两个unique_ptrs指向同一个对象时程序的行为方式?
- 为什么C++中没有常量引用,就像常量指针一样?
- 对于非常量指针类型的参数,未调用具有常量指针模板类型参数的功能
- 将常量指针强制转换为非常量
- 带有常量指针的矢量构造函数示例
- 什么是常量指针常量引用类型的参数?(const X* const & p)
- 在多线程函数中返回共享的常量指针会导致计时问题吗?
- 常量指针上的混乱
- 使用 static_cast 时指向常量指针的原因
- 如何在C++中定义常量指针数组?
- 测试此指针的常量指针性质
- 当常量指针用作函数的参数时
- C++ - 无法将顶部常量指针分配给另一个非常量指针
- 在不同类型之间转换常量指针
- 解构常量指针?
- 为什么 std::vector<>::const_reference 可以转换为非常量指针?