Tech Off Post

Single Post Permalink

View Thread: Andrei, Herb and Scott: Got C++11 Questions?
  • User profile image

    I've got a question regarding a (future, potential) metaprogramming facility -- namely, a possibility of standardizing static loops. It might go hand in hand with adding static conditional expressions (like the proposed static_if) to the future versions of the standard.

    The practical (example) context is Section 15 Metaprogramming of the Agner's optimization manual, "Optimizing software in C++: An optimization guide for Windows, Linux and Mac platforms", see &

    In particular, it discusses examples that "explain how metaprogramming can be used to speed up the calculation of the power function when the exponent is an integer known at compile time."

    "In C++ template metaprogramming, loops are implemented as recursive templates.


    Why is template metaprogramming so complicated? Because the C++ template feature was
    never designed for this purpose. It just happened to be possible.


    The D language allows compile-time if statements (called static if), but no compile-
    time loops. We can only hope that such a feature becomes available in the future. If a future
    version of C++ should allow compile-time if and compile-time while loops, then the
    transformation of example 15.1b to metaprogramming would be straightforward. The MASM
    assembly language has full metaprogramming features, including the ability to define
    function names and variable names from string functions. A metaprogramming
    implementation analogous to example 15.1b and d in assembly language is provided as an
    example in the "Macro loops" chapter in manual 2: "Optimizing subroutines in assembly

    Right now we can (awkwardly) implement conditionals using template specialization and loops using recursive template instantiation.
    I understand the proposed static_if facility is intended to avoid the awkwardness in the case of the compile-time if statements.
    Would it be possible for the next revision of the standard to get an analogous (as described above) compile-time loops extension?