错误!Constexpr变量必须通过常数表达式constexpr初始化
Error! constexpr variable must be initialized by a constant expression constexpr
// The constant base "a" that is being used to compute f_{ut}.
constexpr float A_CONST = 6.76;
// The max number of ratings by any given user on a given date. This
// was found by create_f_u_t.py.
constexpr int MAX_NUM_RAT_USER_DATE = 2651;
// The maximum possible value for f_{ut} is the floor of the log base
// "a" of the maximum number of ratings by any user on a given date.
auto BB = std::floor(std::log(MAX_NUM_RAT_USER_DATE)/std::log(A_CONST));
constexpr int MAX_F_U_T = BB;
它给了我错误!当我编译时,它
说:错误:constexpr变量'max_f_u_t'必须由一个初始化 恒定表达 constexpr int max_f_u_t = bb;
您可以在GCC中获得std::floor
和std::log
的constexpr
版本,但我认为它不是ISO C 。也不要忘记将BB
也是constexpr
。
#include <cmath>
int main()
{
// The constant base "a" that is being used to compute f_{ut}.
constexpr float A_CONST = 6.76;
// The max number of ratings by any given user on a given date. This
// was found by create_f_u_t.py.
constexpr int MAX_NUM_RAT_USER_DATE = 2651;
// The maximum possible value for f_{ut} is the floor of the log base
// "a" of the maximum number of ratings by any user on a given date.
constexpr auto BB = std::floor(std::log(MAX_NUM_RAT_USER_DATE)/std::log(A_CONST));
constexpr int MAX_F_U_T = BB;
}
wandbox上的演示
constexpr变量必须满足以下要求:
- 它的类型必须是文字类型。
- 必须立即初始化
- 其初始化的全表达,包括所有隐式转换,构造函数呼叫等,必须是恒定的表达式
检查constexpr以获取更多详细信息
相关文章:
- (C++)分析树以计算返回错误值的简单算术表达式
- 在VS2010-VS2015下编译时,如何使用decltype作为较大类型表达式的LHS
- 提升精神:解析布尔表达式并简化为规范范式
- 不能在初始值设定项列表中将非常量表达式从类型 'int' 缩小到'unsigned long long'
- 使用正则表达式regex_search在字符串中查找字符串
- 如何确认我的constexpr表达式实际上已经在编译时执行
- 概念中的cv限定符需要表达式参数列表
- 为什么constexpr的性能比正常表达式差
- 对于结构,表达式必须是可修改的ivalue
- 当一个值是非常量但用常量表达式初始化时使用constexpr
- 表达式必须具有常数值,变量不能用作定义数组大小的常数
- 表达式未评估为常数两个级别的constexpr函数(编译器错误?)
- 错误!Constexpr变量必须通过常数表达式constexpr初始化
- 非常量表达式的左值到右值转换的结果是常数吗?
- 如果不在内存中,则在哪里存储表达式和常数
- 现代编译器可以优化表达式从函数得出的常数表达式
- 为什么常数表达式对不确定的行为有排除
- 常数表达式中的条件运算符
- 常数表达式可以是双精度类型而不是只能是整型吗?
- 为什么这不是一个常数表达式