Brian Overland wrote:What is less clear to me is what is the point of a const function if it is not a member function of some class. I may have to do some research on that.
You cannot have a
const function that is not a member of a class, though in C++11 you can have a
constexpr function that is a non-member. If the arguments to a
constexpr function are compile-time constants then the entire function is evaluated at compile time, and the result is also a compile-time constant, which than therefore be used as a template non-type parameter, or the size of an array. Variables of POD types (such as
int or simple classes with no constructors) with static storage duration (globals, namespace-scope variables and local statics) that are initialized with a compile-time constant are statically initialized before any code is run. This can avoid order-of-initialization problems, and of course gives the application a performance
boost as less code has to be evaluated at runtime.
In order to enable this compile-time evaluation, functions declared to be
constexpr must satisfy additional constraints, such as no modifications to variables, no
dynamic_casts, no control flow statements except a single
return statement --- basically the function body and parameter and return types can only use types and operations that can occur in a constant expression.