jww jww - 1 year ago 105
C++ Question

Min/Max/Step function and "body of constexpr function ... not a return-statement"

I'm catching an error when trying to decorate with


$ g++ -std=c++11 test.cxx -o test.exe
test.cxx: In instantiation of ‘static constexpr unsigned int MinMaxStep<min, max
, step>::ValidValue(unsigned int) [with unsigned int min = 10u; unsigned int max
= 100u; unsigned int step = 10u]’:
test.cxx:22:40: required from here
test.cxx:16:5: error: body of constexpr function ‘static constexpr unsigned int
MinMaxStep<min, max, step>::ValidValue(unsigned int) [with unsigned int min = 10
u; unsigned int max = 100u; unsigned int step = 10u]’ not a return-statement

All of the values used in the problem function are template parameters. The values don't change after the file is saved.

Is it not possible to express this as a

If I am doing something wrong, then what is it? How do I modify
into a

$ cat -n test.cxx
1 #include <string>
2 #include <iostream>
4 template <unsigned int min, unsigned int max, unsigned int step>
5 class MinMaxStep
6 {
7 public:
8 static constexpr unsigned int Min() { return min; }
9 static constexpr unsigned int Max() { return max; }
10 static constexpr unsigned int Step() { return step; }
11 static constexpr unsigned int ValidValue(unsigned int v)
12 {
13 if (v <= min) { return min; }
14 else if (v >= max) { return max; }
15 return (v+step-1) - ((v+step-1)%step);
16 }
17 };
19 int main (int argc, char* argv[])
20 {
21 MinMaxStep<10, 100, 10> mms;
22 unsigned int x = mms.ValidValue (18);
23 std::cout << "value " << x << std::endl;
25 return 0;
26 }

Answer Source

The rules for constexpr functions were very strict in C++11. For example there could be only a return statement, nothing else. The rules were relaxed considerably in C++14.

See e.g. this constexpr reference for more information.

There are two ways for you to solve your problem: The easiest is to use C++14 instead (change compiler flag to use -std=c++14). The other solution is to refactor your ValidValue function to only have one single statement, a return statement, using the ternary operator.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download