constexpr initializing static member using static function

前端 未结 4 2118
鱼传尺愫
鱼传尺愫 2020-11-27 22:14

Requirements

I want a constexpr value (i.e. a compile-time constant) computed from a constexpr function. And I want both of these scoped

4条回答
  •  忘掉有多难
    2020-11-27 22:53

    Probably, the problem here is related to the order of declaration/definitions in a class. As you all know, you can use any member even before it is declared/defined in a class.

    When you define de constexpr value in the class, the compiler does not have the constexpr function available to be used because it is inside the class.

    Perhaps, Philip answer, related to this idea, is a good point to understand the question.

    Note this code which compiles without problems:

    constexpr int fooext(int x) { return x + 1; }
    struct C1 {
      constexpr static int foo(int x) { return x + 1; }
      constexpr static int bar = fooext(5);
    };
    
    constexpr static int barext = C1::foo(5);
    

提交回复
热议问题