错误! constexpr变量必须由常量表达式constexpr初始化

时间:2017-05-28 23:45:27

标签: c++ debugging compilation

// 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'必须由a初始化   不断表达       constexpr int MAX_F_U_T = BB;

2 个答案:

答案 0 :(得分:0)

您可以在GCC中获得constexprstd::floorstd::log,但我不认为它是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;
}

Demo on Wandbox

答案 1 :(得分:-3)

  

constexpr变量必须满足以下要求:

     
      
  • 其类型必须是LiteralType。
  •   
  • 必须立即初始化
  •   
  • 其初始化的完整表达式,包括所有隐式转换,构造函数调用等,必须是常量表达式
  •   

检查constexpr了解详情