On Saturday, 11 April 2015 01:22:32 UTC+3, Peter wrote:
> It's going to be a very general question, but here it goes: is template
> metaprogramming a serious programming technique, used in real-life code?
Yes. Every trick (including preprocessor or template metaprogramming)
for to achieve that something is done compile-time or before it
rather than run-time is actually used in real programs written in C++.
> Is there anything in C++ standard that guarantees its validity
> and/or portability (at least to some extent) or is template
> metaprogramming merely a curiosity, an unexpected side effect of
> adding templates to language definition and everything about it is
> either undefined or implementation defined?
Why you ask several, orthogonal to each other questions in same
sentence?
a) Turing-complete set of template metaprogramming techniques is
guaranteed by C++ standard.
b) These techniques are non-portable only to where the compilers
are non-conformant.
c) It is curiosity.
d) It was unexpected side effect of adding templates to language.
e) Some people will always use (either by accident or knowingly)
some undefined or implementation defined effects regardless if
those are related to templates or not.
Important questions you did not ask:
Is metaprogramming in general needed for writing better programs?
No. We can do metaprogramming in our heads. Often we can also let
programs to do things run-time without big disadvantage. It may be
performs slightly worse and gives defect reports bit later.
Is the template metaprogramming needed for writing better programs?
It is not. We can ourselves always use other, nicer means of code
generation.
Is the C++ template metaprogramming needed for writing better
C++ libraries?
Sometimes. Flexibility in integration layer may give wider
usability. Early diagnostics are good so user does not waste his
time to trying to misuse the library and what little is
performance-critical for user may differ per user. The C++
preprocessor metaprogramming is even worse and delivering
separate tools with library adds to complexity of its usage.