3 ms·
> For most people "reflection" is a synonym to "runtime reflection". I mean, that's just because many languages don't even have a compile-time so of course ref
by jcelerier 4y ago
> For most people "reflection" is a synonym to "runtime reflection".
I mean, that's just because many languages don't even have a compile-time so of course reflection can only be runtime. Every compiled language has at least some amount of extremely basic concept of compile-time reflection, e.g. sizeof in C for instance which yields a constant expression ; likewise, `if constexpr(std::is_same_v<T, some_other_type>) { ... }` which we can do since C++17 is very much in the domain of compile-time reflection.
Here's the most recent paper: https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p1240r2.pdf https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p12...
If you want this is a very good read: https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0707r3.pdf https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p07...
Finally, there is an implementation of the reflection TS: https://clementpirelli.wordpress.com/2021/12/08/cpp-reflection-ts-first-look/ https://clementpirelli.wordpress.com/2021/12/08/cpp-reflecti...
- still_grokking 4y agoOh, cool, direct links to the relevant papers! It's not always easy to find the relevant stuff in C++ land. Thanks a lot! I have to read this stuff first, but my gut feeling would still be that `constexpr` and such isn't reflection at all. The `sizeof` example seems more in this direction (because some expression gets actually inspected).
- jcelerier 4y agoi'm talking about "if constexpr", not "constexpr" ; "if constexpr" allows to make choices at compile time depending on properties of a variable and that is definitely what reflection is about. e.g. void f(auto x) { // common code path if constexpr(x is floating-point) { // code path 1 } else if constexpr(x is a string) { // code path 2 } // common code path }
- still_grokking 4y agoOh, I see. You're right. Thanks for the pointer.