jeudi 30 avril 2020

constexpr array of constexpr function pointers

I'm trying to get my head around how constexpr work. And I need to convert a lot of code from const to constexpr. But I've hit a problem where I cant see the solution.

I have the following class:

class Control_base
{
public:

  static constexpr Control_base high_clock();

  static constexpr uint8_t size();

  static const Control_base from_index(uint8_t index_);

  Control_base& operator=(const Control_base& rhs) = default;

  constexpr Device_address value() const;
private:
  constexpr explicit Control_base(Device_address value_);

  Device_address value;


};

constexpr Control_base::Control_base(Device_address value_) :
  value(value_) {}

constexpr Device_address Control_base::value() const { return value; }

inline const Control_base Control_base::high_clock() { return Control_base(reinterpret_cast<Device_address>(0x10009000)); }

typedef const Control_base (*Control_base_func)(void);

const Control_base_func Control_base_arr[] = {&Control_base::high_clock};

inline const Control_base Control_base::from_index(uint8_t index_)
{
  return Control_base_arr[index_]();
}

constexpr uint8_t Control_base::size() { return 1; }

};

I wish to make the following changes:

From

inline const Control_base Control_base::high_clock() { return Control_base(reinterpret_cast<Device_address>(0x10009000)); }

typedef const Control_base (*Control_base_func)(void);

const Control_base_func Control_base_arr[] = {&Control_base::high_clock};

To

constexpr Control_base Control_base::high_clock() { return Control_base(reinterpret_cast<Device_address>(0x10009000)); }

typedef const Control_base (*Control_base_func)(void);

constexpr Control_base_func Control_base_arr[] = {&Control_base::high_clock};

However, I get the following error in

constexpr Control_base_func Control_base_arr[] = {&Control_base::high_clock};
                                                  ^

**value of type "ns::Control_base (*)()" cannot be used to initialize an entity of type "const ns::Control_base_func"**

I can't figure out what the best solution is here. And why it works with const but not constexpr

Regards

Aucun commentaire:

Enregistrer un commentaire