I know the ISO C standard make a big deal about separating translation behaviour and execution behaviour, partly to ensure cross-compilers don't have to carry the execution environment of every target.
By that, I mean the compiler has limited information available to it compared to a running program. That limits what you can do in the source code, such as not initialising a variable based on the return value from a function like this:
int twice (int val) { return val * 2; }
int xyzzy = twice (7);
int main () { printf ("%d\n", xyzzy); return 0; }
What I'm curious about is how user defined literals in C++11 fit into this scheme. Since the literal evaluation relies on a function, what's to stop that function doing things like:
- returning a random value (even if based on the input such as
42_roughly
giving you a value between 40 and 44)? - having side effects, such as changing global variables?
Does the fact that a function have to be called means that these aren't really literals in the sense of being calculated at compile time?
If that's the case, what's the advantage of these literals over any other function call. In other words why is:
int xyzzy = 1101_1110_b;
preferable to:
int xyzzy = makeBin ("1101_1110");
?