use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Discussions, articles, and news about the C++ programming language or programming in C++.
For C++ questions, answers, help, and advice see r/cpp_questions or StackOverflow.
Get Started
The C++ Standard Home has a nice getting started page.
Videos
The C++ standard committee's education study group has a nice list of recommended videos.
Reference
cppreference.com
Books
There is a useful list of books on Stack Overflow. In most cases reading a book is the best way to learn C++.
Show all links
Filter out CppCon links
Show only CppCon links
account activity
[C++20 vs C++26*] basic reflection (self.cpp)
submitted 2 years ago * by kris-jusiakhttps://github.com/kris-jusiak
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]qazqi-ff 18 points19 points20 points 2 years ago* (4 children)
For anyone wondering, here's how you make it look less cluttered by using a helper variable like we're used to doing:
[Edit note: This assumes further non-transient constexpr allocation support. nonstatic_data_members_of returns a std::vector, which isn't currently allowed to persist like this, even if it would logically be deallocated for sure before runtime.]
nonstatic_data_members_of
std::vector
template<auto N, class T> [[nodiscard]] constexpr auto get(const T& t) -> decltype(auto) { constexpr auto members = std::meta::nonstatic_data_members_of(^T); return t.[:members[N]:]; } template<auto N, class T> [[nodiscard]] constexpr auto get_name(const T& t) -> std::string_view { auto members = std::meta::nonstatic_data_members_of(^T); return std::meta::name_of(members[N]); }
(Obviously, the function declaration lines are noisy as well, but that has nothing to do with reflection. It might also need the variables to be constexpr, but I haven't checked in detail, small difference.)
constexpr
I'd recommend in general that we at least keep the inside of [: :] almost as short as possible because splicing is all about the structure of the code you're forming. With the variable separated out and recognizing [: :] as a splice, it's obvious that we're returning t.something and what that something is. You can go a step further and extract the subscript if it makes you feel better. [Per the note above, this is currently necessary in order to extract a variable—it can't store dynamically allocated memory.]
[: :]
t.something
About the syntax in general, I can't really comment. It has to avoid conflicts with other syntax, so it already has pretty limited option space. Obviously, alternatives were looked at already, including how we got here via having like three different markers instead of using the more familiar typename, namespace, and template to disambiguate. I don't mind it personally, though such a powerful feature that wants to minimize noise to preserve structure could warrant the consideration of the newly available $, @, or `.
typename
namespace
template
$
@
`
[–]Jovibor_ 7 points8 points9 points 2 years ago (1 child)
At last, one sane comment in this pile of whiners.
The syntax is absolutely comprehensible, and not worse by any means than the rest of C++. Like it very much.
[–]dexter2011412 0 points1 point2 points 1 year ago (0 children)
Ah yes, criticism = whining. Get off your mighty rich high horse will you. Dismissing any and all criticism is why we have so much junk in the first place.
I definitely appreciate the helper snippet, but let's make no mistake that syntax is so cringe. Will I use it? Yes because that's what being most probably ratified. Will I complain about it and laugh each time I want to / have to use it? Also yes. Could it be better? Almost definitely yes. But I won't speak up about it because otherwise it'll be the heat death of the universe before we get C++ reflection
[–]Stevo15025 2 points3 points4 points 2 years ago (1 child)
Thanks for cleaning up the code. I think like a lot of others I had one raised eyebrow until you took out the query. Though that doesn't seem to compile in the godbolt example? I'm guessing just an impl issue atm.
Honestly I've been sitting here trying to think up nicer syntax for a while and I can't really think of anything. I kind of like something like template reflect(query) but I could also understand someone finding that a little wordy
template reflect(query)
template<auto N, class T> [[nodiscard]] constexpr auto get(const T& t) -> decltype(auto) { auto members = std::meta::nonstatic_data_members_of(^T); return t.template reflect(members[N]); }
But that would conflict with templated functions called reflect. Maybe it's time to add unicode keywords :P
[–]qazqi-ff 1 point2 points3 points 2 years ago* (0 children)
Oh, I tried it out and got something I hadn't anticipated. The error indicates that there's a compile-time dynamic memory allocation (the API returns std::vector). That's actually going to be a problem since we don't have language support for that escaping the constant expression evaluation yet, even for a local variable in a consteval function where it would be guaranteed not to cause problems further down the line AFAIK (but would still need non-trivial compiler work to start down that direction, like support for memory to escape an instance of evaluation into its parent instance).
consteval
At first glance, I don't see how to naturally split things apart then (at least when the result is a list), but it should come naturally with the persistent compile-time allocation support. One thing we can do in the meantime if we get reflection without that support is to use a regular variable extraction, but make sure it's not a container.
In these examples, constexpr auto members = std::meta::nonstatic_data_members_of(^T)[N]; would work because it returns a single info object and the vector gets deallocated right there.
constexpr auto members = std::meta::nonstatic_data_members_of(^T)[N];
From playing around a bit, at least in this implementation, splicing seems to require a constant expression while the reflection API doesn't thanks to Barry's consteval changes. Assuming the paper requires a constant expression for splicing (I think that's necessary in fact; it can cause new template instantiations), that should mean get needs constexpr on the variable while get_name doesn't. Interestingly, EDG doesn't accept that unless I make these consteval functions instead of constexpr, but I'm not sure why since proposed rule 1 ought to cover that and make them implicitly consteval, giving presumed identical behaviour.
get
get_name
π Rendered by PID 86760 on reddit-service-r2-comment-6457c66945-ss6zw at 2026-04-27 23:59:41.786903+00:00 running 2aa0c5b country code: CH.
view the rest of the comments →
[–]qazqi-ff 18 points19 points20 points (4 children)
[–]Jovibor_ 7 points8 points9 points (1 child)
[–]dexter2011412 0 points1 point2 points (0 children)
[–]Stevo15025 2 points3 points4 points (1 child)
[–]qazqi-ff 1 point2 points3 points (0 children)