0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

constexpr virtual 関数におけるMSVCのバグ

Posted at

C++20から、仮想関数にconstexprを指定することができるようになりました。

そこで、constexpr virtual関数を使ってコードを書いていたのですが、gccとclangではコンパイルが通るのに、MSVCではコンパイルエラーになるという現象に悩まされていました。色々と検証した結果、constexpr virtual関数からメンバ変数にアクセスするとMSVCでだけ定数として評価できないようです。
再現コードは以下になります。

struct base
{
    constexpr virtual int value() const = 0;
};

struct derived : public base
{
    constexpr int value() const override { return m_val;}
    int m_val = 42;
};

Visual StudioのDeveloper Communityで検索した結果、これは報告済みのバグで、現在(2024/9/18)でも修正されていません。

上記のDeveloper Communityにもあるように、関数ポインタを通して呼び出すことによって、constexpr関数を使うことができるのですが、煩雑極まりないので早く修正されてほしいです。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?