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

标签 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;

它给我错误!当我编译时,它

says: error: constexpr variable 'MAX_F_U_T' must be initialized by a constant expression constexpr int MAX_F_U_T = BB;

最佳答案

您可以在 GCC 中获得 std::floorstd::logconstexpr 版本,但我不认为它是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

关于c++ - 错误! constexpr 变量必须由常量表达式 constexpr 初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44233038/

相关文章:

c++ - 关于以二进制存储并显示其十进制

Android WIFI ADB 插件不适用于某些 Android 真实设备?

c# - 如何在我的 .csproj 文件中包含 DLL?

c++ - 如何在 Mac 上使用 clang 构建我的 C++ 代码?

c++ - vector 类 : Difference between 4 floats, 或 4 个 float 的数组

visual-studio - VS 2008 调试器以十六进制显示整数

c++ - 为什么 GLU 会在这个地方崩溃?

python - 在 linux 上使用 pyinstaller 将 python 编译为二进制文件时未定义退出

java - 如何从源码制作midlet?

c++ - 如何使用条件运算符?