What is constexpr in C++?

前端 未结 4 2233
不思量自难忘°
不思量自难忘° 2021-02-07 07:48

I am really confused about a constexpr concept, as I have read constexpr is evaluated at compile time, so it is useful for performance optimization ver

4条回答
  •  故里飘歌
    2021-02-07 08:10

    Here are my 2 cents:

    The constexpr feature defines computation that happens during the compile time. Reasonable question:

    • Does it make sense to allow any computation?

    Reasonable answer:

    • No, because this would require a lot of machinery, resources, etc right inside the compiler while there is no direct need for that.

    Because of that standard allows pretty limited set of features inside the constexpr code. One may argue why exactly this set and not something more? Well, later on the standard may evolve and allow more.

提交回复
热议问题