0

I have a class which contains three static constants,

static const int NUM_POINTS = 2000;
static const float LAKE_THRESHOLD = 0.3;
static const int NUM_LLOYD_ITERATIONS = 2;

In the header file. I realize that now in C++11 I have to use a constexpr but I can't figure out how to use them. Can anyone explain constexpr in a simple way?

1 Answers1

0

constexpr can be used to mark an expression as compile-time constant. It extends to functions as well, so an arbitrarily deep call chain can be compile-time constant. This permits the compiler to substitute the constant value, rather than evaluating it unnecessarily at runtime.

See: http://en.cppreference.com/w/cpp/language/constexpr

Scott Jones
  • 2,880
  • 13
  • 19
  • Ok so now I have this declared in the header: `constexpr static float FRACTION_LAVA_FISSURES() {return 0.2;}` Then in the .cpp I have this: `if ((!edge.river) && (!edge.d0.water) && (!edge.d1.water) && (edge.d0.elevation > 0.8) && (edge.d1.elevation > 0.8) && (edge.d0.moisture < 0.3) && (edge.d1.moisture < 0.3) && ((float)(rand() / RAND_MAX) < FRACTION_LAVA_FISSURES))` It says `invalid operands of types 'float' and 'float()' to binary 'operator<'` –  May 03 '13 at 02:09