Can compile-time computation be considered a key advantage of C++ v. C ?
Actually, what is important is not compilation, but software building.
Refer to Wikipedia page on build automation.
Then, be aware that many software projects (including many open source projects on github or gitlab) are generating C (or even C++) code from something more abstract (e.g. using software tools). A typical example is obviously parser generators (a.k.a. compiler-compilers) like GNU bison or ANTLR. Another example are glue code generators like rpcgen
or SWIG. And GNU autoconf adapt your build to the software packages available on your computer. Notice that both Chicken-Scheme and Bigoo are generating C code from Scheme source code. See of course this. In some cases huge C files are produced from tiny input (see also the XBM format). Maple is able to generate large C files, and there are cases where generating a lot of C code -e.g. half a million lines- makes sense (as explained in Pitrat's book Artificial beings: the conscience of a conscious machine) and blog.
At last, whole-program optimization can exist (see the -flto
flag in recent GCC for Link-Time-Optimization; you practically would compile and link with gcc -Wall -O2 -flto
) and requires some compiler support at "link-time".
In some situations, the compile time is not that important (think of e.g. compiling Firefox or the Linux kernel or LibreOffice or Gnome or GTK from its source code base), but the build time can last hours, or certainly dozens of minutes (because a lot of different translation units - concretely *.c
or *.cc
files - have to be compiled then linked).
Google is rumored to consume hours of computer time internally to build most of their internal software.
Notice that the first C++ compilers (e.g. Cfront) have been implemented as C code generators, and that a large software such as the GCC compiler has dozens of specialized C or C++ code generators. Try to build on your laptop from the available source code a GCC cross-compiler targeting your RaspBerryPi board (which is too small and underpowered to straight-compile GCC on it). Build instructions on LinuxFromScratch are then relevant.
For an example of a C program generating C code, see my manydl.c code for Linux, or my Bismon program described in this draft report. Past versions of the obsolete GCC MELT project did generate a million lines of C or C++ code. manydl.c
is able to generate then compile C code during days, and illustrates that dlopen(3) can be used many times. For an example of a C++ software generating C++ on Linux, see my RefPerSys project. Look also into tunes.org for discussions related to metaprogramming and generation of C or C++ code.
e.g. compiling C code for an Arduino, or C++ code for your RaspberryPi on your laptop, perhaps with GCC. Or compiling on your PC code for a distant top500 supercomputer.
regarding C++ versus C
My understanding of the C++ standard n3337 is that compile-time computation is not specified there (but I don't claim to be a C++ expert).
In particular, nothing forbids you to make your C++ interpreter (you could code that in C, in C++, in Ocaml, in Java, etc...). Consider that idea as an interesting programming exercise (but read the Dragon book before trying).
My opinion is that a classroom of students learning C++ could be considered as a C++ implementation, as specified in that C++ standard. A good way of teaching C++ is to ask the classroom about the semantics of several C++ programs, and that can be taught with pencil and paper or a whiteboard. I actually taught a course on operational semantics that way (at Paris 6 University).
The board was black, and I used chalks of various colors.
Look also into software tools like Frama-C or Clang static analyzer. Both are open source, so you could study their source.
Is this this one manner in which a C++ programs can achieve better performance (e.g. speed) versus an equivalent C program ?
That it your opinion, and I disagree. What make you think that the runtime of Ocaml or of SBCL would be faster (you should download and study the source code) if it has been written in C++ ? An interesting exercise could be to recode in C++ the tinyCC compiler (for C, targeting x86 32 bits and x86-64 bits on Linux, coded in C), and benchmark any improvement. That simple but clever compiler is compiling C code very quickly, but make too few compiler optimizations.
constexpr
orconsteval
does not mean that the compiler can’t do the computation at compile time if all values are known. With the keyword you tell the compiler what the intent is, so that the compiler can warm you about a mistake you made to ensure your intent. – Wagramconstexpr
doesn't guarantee compile-time computation, either. – Ashramconstexpr
. Also, I agree that the keyword does not mean compile-time evaluation in all cases. However, as far as I am aware, it does guaruntee compile-time evaluation when the declared function is called with constant expression arguments. – Oneupmanship