定义constexpr函数的规则:
1、形参和返回值类型都是字面值类型(基本类型)
2、函数体有且只有一条return语句。
constexpr int new_sz()
{
return 42;
}
constexpr size_t scale(size_t cnt)
{
return new_sz()*cnt;
}
不一定返回常量表达式
所有对象都是字面值常量,才是真的常量表达式。
int arr[scale(2)];
int i = 2;
int a[scale(i)]; //没有xxx,虽不是常量表达式,但也能用于数组
int a1[i]; //连变量都能用来当数组大小了
constexpr size_t s1 = scale(2);
constexpr size_t s2 = scale(i); //xxx,确实不是常量表达式