C++11 - Can't define constexpr literal using constexpr function?

前端 未结 2 828
孤街浪徒
孤街浪徒 2021-01-19 23:38

I\'ve run into what seems a counterintuitive error, namely, the inability to assign the value of a constexpr function to a constexpr literal (hope

2条回答
  •  梦谈多话
    2021-01-20 00:05

    In C++, inline definitions of member functions for a class are only parsed after the declaration of the class is complete.

    So even though the compiler "knows" about MyClass::FooValue(int), it hasn't "seen" its definition yet, and hence it can't be used in a constexpr expression.

    A general workaround for this is to stick to constexpr member functions, or declare constexpr constants outside the class.

提交回复
热议问题