constexpr

Hi,

Please consider this simple code:

 
  constexpr double square(double x) { return x*x; }


Now we use it this way:

1
2
  int var = 17;
constexpr double max2 = 1.4*square(var);


why is it incorrect, please?
Last edited on
Maybe because the int isn't const?
1
2
3
4
5
6
7
$ g++ -std=c++11 main.cpp
main.cpp:10:39: error: the value of ‘var’ is not usable in a constant expression
 constexpr double max2 = 1.4*square(var);
                                       ^
main.cpp:9:5: note: ‘int var’ is not const
 int var = 17;
     ^
You cannot use var in a constant expression unless it's a constexpr constant.
And please don't crosspost next time.

http://www.cplusplus.com/forum/general/247794/
Topic archived. No new replies allowed.