本文介绍了一个静态const和constexpr变量有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我明白, constexpr
变量可以在compiletime使用。
对于一个模板或者静态声明为实例。
I understand that a constexpr
variable can be used at compiletime.For a template, or static asser for instance.
但是如果我想这样做没有constexpr我可以用 static const
。
But if I want to do that without constexpr I can with static const
.
什么是因为C ++ 11/14引入了constexpr
What is since C++11/14 introduced constexpr the difference between
constexpr int a = 3;
//AND
static const int a = 3;
谢谢!
推荐答案
我知道的主要区别是,必须在编译时知道 constexpr
的值,而在运行时可以分配 const static
。
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();
这篇关于一个静态const和constexpr变量有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!