mathstodon.xyz is one of the many independent Mastodon servers you can use to participate in the fediverse.
A Mastodon instance for maths people. We have LaTeX rendering in the web interface!

Server stats:

2.9K
active users

Say I want to detect if a struct has a field `size` of type `int`. Create two template instantiations of the same name, here `HasStaticSize` that defaults to false.

The latter is only resolved if `T::size` is declared as `int`, or more specifically, something that "decays" (en.cppreference.com/w/cpp/type) to `int`. The outcome of these two is inheriting from structs whose `::value` member is constant `true` or `false`.

en.cppreference.comstd::decay - cppreference.com

Then you can use `constexpr` in an `if` to branch on the check (`::value` extracts a boolean member from the templated struct), and behave differently based on the result.

Note removing the `decay_t` will change the result, because apparently `decay` removes `const`. You could also remove it and compare to `const int`.

This is all weird and I don't really like C++ template metaprogramming.

@j2kun D's metaprogramming is lovely and I wish other languages would have it. It was the only reason I ever tried it.

I guess I should really get into lisp one day but D had almost everything I ever wanted.