D
David Brown
I don't think I'm that comfortable with the idea.
If you did need something such as a set of pre-calculated tables, then
you might just use a script language to generate C code in an include file:
That can certainly be done - and it is a method I often use.
- You keep both languages simple
constexpr doesn't really make C++ any more complex - it just makes the
existing compile-time calculations more explicit.
- You don't have to re-run the calculations on every compilation (which
can include running the user's possibly slow, buggy code with the
likelihood of hanging the compiler is something is not right)
If the user's code is buggy, it is buggy no matter where it is executed.
And "make" will let you avoid re-generating your tables if they have not
changed.
- You use a tool (the script language) which can be more appropriate for
the job, especially if the output will be a string.
Sometimes that's true. But in cases where the pre-calculated data could
be generated within C++, keeping everything within the same language can
be a better choice.
- It easier to generate multiple values (ie. a table) rather than the
single-value constant definer we've been talking about
This is the same language where we won't even have official binary
constants until 2017?
Yes, though I don't quite see the connection. I don't claim that
constexpr makes C++ a "complete" or "perfect" language - it has plenty
of flaws. The same applies to C.
(gcc, and some other compilers for embedded targets, have had binary
constants for a good while now. It's an extension rather than a
standard feature.)
And the same one where you already /can/ define 'static const int
sizemax=1024', but it's not possible to use that to dimension an array?
C++ allows you to use static const ints as the dimension of an array.
I think maybe it ought to learn to walk before it can run!
I would like C to enhance its existing "const" (which was originally
copied from C++) to allow its use in array dimensions, just like in C++.
I cannot think of any reason why this is not in the C standards, and it
is a feature that would improve many C programs.
Copying "constexpr" from C++ into C would be less useful, but still have
its place - and it would be possible without "corrupting" C with too
much C++ style. Of course, in C it would be called "_Constexpr", but we
are used to that.