There is a feature in c++ for forcing compile-time executions of functions on demand, if that is possible.
The feature is constexpr
functions.
Now at the committee they are trying (I think this is Niccolas Josuttis effort) to give
guidelines on when to use constexpr, since in C++14 they relaxed constexpr
and there
is a potential "risk" to use constexpr
everywhere.
I wonder, in the first place, why it was decided that functions are decorated with constexpr
.
In D you can do this:
//No special signature, as in C++'s constexpr
uint factorial(uint n) { ...}
//Calculate at compile-time. Note that the request is just made at the point of use
static val = fac(5);
- Why in C++ it was decided that you must decorate the function signature in the first place?
- Could this rule be relaxed for C++17? This would eliminate the need to design a guideline and
would make it more flexible. You would use
constexpr
where you usestatic
in D and get rid of the other part.