1. 2

Just my opinion, but I found it hard to figure out the variadic template loop. In python it’s so simple just as you’d expect, but c++ makes it difficult. Or maybe it’s just different and I’m not used to that way of thinking yet.

Does anyone here know why there is no simple loop in c++ as in python?


  2. 5

    There are some fundamental under-the-hood differences between using a C++ variadic template and a variadic Python function, one is that that the C++ version is type-safe and checked at compile time.

    C++ for loops were traditionally a run-time loop and so they don’t apply to parameter packs which are a compile-time construct only. While a for loop can now be evaluated at compile time (in a constexpr function) their syntax and semantics are unchanged in that case, i.e. they still apply to constructs that are (or at least which can also be) run-time. Allow for loops to iterate through parameter packs would require new syntax and semantics in a language that many feel is already getting too complex. That said I suspect you could do this with something more like a regular loop with C++2x features (or possibly even C++17, I haven’t been keeping up to well) e.g. with template lambdas.

    But in general, if you’re looking for C++ to be as concise as Python, you’re going to be disappointed. With C++ you get better performance and compile-time type checking, at the cost of a complex syntax, often more verbose code, lack of memory-safety, and the need to compile before executing.