constexpr initializing static member using static function

前端 未结 4 2113
鱼传尺愫
鱼传尺愫 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:39

    #include 
    
    class C1 
    {
    public:
        constexpr static int foo(constexpr int x)
        { 
            return x + 1;
        }
    
        static constexpr int bar;
    };
    
    constexpr int C1::bar = C1::foo(sizeof(int));
    
    int main()
    {
        std::cout << C1::bar << std::endl;
        return 0;
    }
    

    Such initialization works well but only on clang

提交回复
热议问题