What is the difference between a static const and constexpr variable?

£可爱£侵袭症+ 提交于 2020-01-02 03:24:07

问题


I understand that a constexpr variable can be used at compiletime. For a template, or static asser for instance.

But if I want to do that without constexpr I can with static const.

What is since C++11/14 introduced constexpr the difference between

constexpr int a = 3;
//AND
static const int a = 3;

Thank you!

Another way to see this question is which should I use?


回答1:


The main difference that I know is, the value of constexpr must be known in compile-time while a const static can be assigned in run-time.

const static int x = rand();


来源:https://stackoverflow.com/questions/23538440/what-is-the-difference-between-a-static-const-and-constexpr-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!