Why is C++11 constexpr so restrictive?

后端 未结 5 973
死守一世寂寞
死守一世寂寞 2020-12-15 02:27

As you probably know, C++11 introduces the constexpr keyword.

C++11 introduced the keyword constexpr, which allows the user to guarante

5条回答
  •  情话喂你
    2020-12-15 02:58

    I think constexpr is just for const objects. I mean; you can now have static const objects like String::empty_string constructs statically(without hacking!). This may reduce time before 'main' called. And static const objects may have functions like .length(), operator==,... so this is why 'expr' is needed. In 'C' you can create static constant structs like below:

    static const Foos foo = { .a = 1, .b = 2, };
    

    Linux kernel has tons of this type classes. In c++ you could do this now with constexpr.

    note: I dunno but code below should not be accepted so like if version:

    constexpr int maybeInCppC1Y(int a, int b) { return (a > 0) ? (a + b) : (a - b); }
    

提交回复
热议问题