@cyberpunk_ is trying to achieve something and made some questions about it but all the chase boils down to this:
Is it possible to build a tool to enforce compile-time evaluation of a constexpr
function?
int f(int i) {return i;}
constexpr int g(int i) {return i;}
int main()
{
f(at_compilation(g, 0));
int x = at_compilation(g, 1);
constexpr int y = at_compilation(g, 2);
}
In all situations, at_compilation
enforce compilation-time evaluation of g
.
at_compilation
doesn't need to be in this form.
Requirements
- Allow any (numerical native) literal type as input for the constexpr function.
- this could also be hardcoded based on the function arguments types.
- Allow any (numerical native) literal type as output, which is the result of the constexpr function call.
- this could also be hardcoded based on the function return type.
Desirables
- Reduced macro usage but don't be afraid of using.
- Be general (not type hardcoded).
- Support any literal type. At last any numerical native literal type is a requirement.
Related Questions:
- When does a constexpr function get evaluated at compile time?
- Forcing a constant expression to be evaluated during compile-time?
- Passing any function as a template parameter?
- Where in the C++11 standard does it specify when a constexpr function can be evaluated during translation?
Answers with relevant code samples:
All the code samples have limitations regarding the requirements.
A clear explanation for how this is unfeasible in C++ is also a good answer.
I suspect it's impossible based on @K-ballo / @Herb Sutter answer which states "and the result is used in a constant expression as well". This was not part of my former conception about constexpr
functions, I firstly thought that just passing literals (or other compile-time input) as arguments would suffice to guarantee (by standard) it to be evaluated at compilation-time.
It's already assumed constexpr function's purpose is that they can fit in constant expression situations when necessary, like in array bounds. That's OK. Given that, this question is about a hack on using them just as a tool for compile time calculation. Whether it's a good or bad thing to do should not matter.
See Question&Answers more detail:os