Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;

It gives me error! When I compile, it

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

like image 685
kuku Avatar asked Mar 14 '26 01:03

kuku


1 Answers

You can get constexpr versions of std::floor and std::log in GCC, but I don't think it is ISO C++. Also don't forget to make BB a constexpr as well.

Update 2025/06/24: std::floor is constexpr starting with C++23, std::log will be constexpr starting with C++26.

#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

like image 75
Henri Menke Avatar answered Mar 15 '26 16:03

Henri Menke



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!