News for hackers. Stuff that matters.
Re:An opinionated history of programming languages by lalaithion on Saturday October 03, 2020 @05:34PM (#24674826)
Anyone care to explain what

    template <size_t i, typename... Ts, typename CurTy>
    void recurseFillChildren(CurTy &E)
    {
      using PackTy = std::variant<Ts...>;
      using TyL = std::variant_alternative_t<i - 1, PackTy>;
      static_assert(std::is_same_v<CurTy, TyL>);
      using TyR = std::variant_alternative_t<i, PackTy>;

      for (i32 j = 0; j < E.NChildren; ++j)
      {
        E.Children.push_back(miniParser<TyR>());
        if constexpr (i + 1 < sizeof...(Ts))
          recurseFillChildren<i + 1, Ts...>(E.Children.back());
      }
    };
does, and why it can't be translated into Rust?
Re:An opinionated history of programming languages by neutronicus on Saturday October 03, 2020 @11:40PM (#24676368)
If you have a hierarchy of types

    struct A {vector<B> Children; int NChildren = 2, ...};
    struct B {vector<C> Children; int NChildren = 2; ...};
    struct C {vector<D> Children; int NChildren = 2; ...};
    struct D {...)
and a function template

    template<typename T> T miniParser() { return T{}; }
and you call it like

    struct A;
    recurseFillChildren<A, B, C, D>(A);
It expands into nested loops that fill the "Children" areas of each type with two default-constructed values of the next type down the hierarchy, until you get to D, at which point it does nothing.

The reason you can't implement this in Rust is that Rust Generics (their "template-y" language feature) is missing a few capabilities relative to C++ templates. Namely,

1. They don't operate on numbers

2. They have no notion of collections of types

Because of this, they

1. Can't know that there is a collection of types

2. Can't extract the size of the collection of types

This comes up in numerical code, where in C++ it is easy to write e.g. geometry code that is generic in the number of spatial dimensions, but in Rust it is a pain in the ass.

Re:An opinionated history of programming languages by steveklabnik on Saturday October 03, 2020 @06:27PM (#24675144)
This is a "non-type template parameter," that is, it's generic over that size_t i. Rust cannot yet do this. This is "const generics" in Rust speak.
Re:An opinionated history of programming languages by UncleOxidant on Saturday October 03, 2020 @05:36PM (#24674836)
I used to know C++. But not sure what's happening here.
Re:An opinionated history of programming languages by aninteger on Saturday October 03, 2020 @09:09PM (#24675899)
This is me too and sometimes I feel like the language is going in two directions. The academic / scholar path where people show off unreadable C++ like this and the business path where professionals that use a subset of the language (mandated by corporate code base or teams) that are competent C++ developers but maybe not writing this complicated C++.
Re:An opinionated history of programming languages by renox on Sunday October 04, 2020 @03:04AM (#24677039)
I work in telecoms and 'template like this' show up from time to time sometimes for reasonable reasons, sometimes for 'optimisations' reasons which are not backed by figures (some programmers like to show off).
Re:An opinionated history of programming languages by stephc_int13 on Saturday October 03, 2020 @10:50PM (#24676198)
Templates, in my opinion, are often extremely hard to read, not unlike Perl in its time.

And I will always prefer more verbose code that I can read, understand and maintain.

Re:An opinionated history of programming languages by ncmncm on Saturday October 03, 2020 @08:08PM (#24675661)
It demonstrates a method of generating a family of types or functions that, until C++17, would have required Template Metaprogramming via partial specialization or template function overloading, but is now just programming.

The key line is "if constexpr", which is evaluated at compile time and terminates the recursion.

Re:An opinionated history of programming languages by gameswithgo on Saturday October 03, 2020 @09:54PM (#24676050)
could a proc macro not do it?