簡體   English   中英

constexpr function 指針的 constexpr 數組

[英]constexpr array of constexpr function pointers

我試圖弄清楚 constexpr 是如何工作的。 而且我需要將大量代碼從 const 轉換為 constexpr。 但是我遇到了一個我看不到解決方案的問題。

我有以下 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; }

};

我希望進行以下更改:

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};

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};

但是,我收到以下錯誤

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"**

我無法弄清楚這里最好的解決方案是什么。 以及為什么它適用於 const 但不適用於 constexpr

問候

解決了,我忽略了我已經將返回類型從 const 更改為 non-const

它應該看起來像這樣

constexpr const 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};

問候

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM