4 ms·
Anyone care to explain what template <size_t i, typename... Ts, typename CurTy> void recurseFillChildren(CurTy &E) { using PackTy = std::vari
by lalaithion 6y ago
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?
- UncleOxidant 6y agoI used to know C++. But not sure what's happening here.
- aninteger 6y agoThis 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++.
- renox 6y agoI 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).
- stephc_int13 6y agoTemplates, 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.
- steveklabnik 6y agoThis 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.
- ncmncm 6y agoIt 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.
- gameswithgo 6y agocould a proc macro not do it?
- neutronicus 6y agoIf 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.