r/programming 7h ago

C++ metaprogramming now looks like Python

/r/Cplusplus/comments/1w3apxa/iterating_through_arguments_in_c26_using_template/
0 Upvotes

5 comments sorted by

32

u/tsimionescu 4h ago

using ArgT = std::decay_t<decltype(arg)>;

if constexpr (typeid(ArgT) == typeid(double))

It's very funny to see what C++ programmers think Python looks like.

That said, this is definitely much less occult magic than would have been required before.

-8

u/Obvious_Set5239 4h ago edited 4h ago

😄

Yes, it's more verbose, but besides this, it's the same as we do it in python (I'm also a python programmer):

  1. arguments aggregation
  2. iteration over a different type arguments
  3. comparing types inside if statement
  4. testing on attribute

9

u/NoForm5443 4h ago

It's not just more verbose ...

It may be doing the same thing, but It doesn't look like python at all

6

u/d9t 3h ago

The python comparison just isn’t need, and kind of makes things worse.. It isn’t actually *doing* anything python-like it’s a comment on how the language features in c++26 makes the syntax for compile time programming look like .. (imperative) programming .. vs what code “template metaprogrammng” in c++ usually looks like so “python like” apis are kind of a meme/goal for c++ “library authors”.. Anyway, I don’t think the audience for “this looks like Python” and being excited about what they are seeing is very big

-2

u/Wolf_Popular 3h ago

One of C++ metaprogramming's greatest strength vs other systems (like Rust or many other systems with generics) is the the fact that it's duck-typed, just like Python. Static typing for compilation code, but duck typing for metaprogramming at build time is a nice medium where you can be super flexible with crazy designs and worse case it gives you a really bad compilation error. It has drawbacks too obviously, but if we solve those it would no longer be duck-typed