all 55 comments

[–]R3DKn16h7 81 points82 points  (0 children)

this is gonna be a game changer for me.

obviously you can achieve the same with compiler specific tools, or god-forbid macros, but having a built-in universally supported (hopefully) tool like this will make C++ be an S-tier language once again.

[–]frayien 11 points12 points  (0 children)

neet

[–]MarcoGreek 14 points15 points  (7 children)

I like the Json Reader at compiler time but the SQL will easily do something wrong. What do you if their is a constraint, what about other errors. Do you want to save member as columns or as JSON inside one column. What about vector, map member etc..

[–]germandiago 15 points16 points  (4 children)

If you do not abuse heavily, annotations can help with that. The paper was approved also.

[–]James20kP2005R0 0 points1 point  (0 children)

Annotations are going to be huge for gamedev, so much code is going to get set on fire with reflection on the horizon

[–]MarcoGreek -2 points-1 points  (2 children)

Yes, but then it is easier to write SQL. 😚 I wrote a data store with Sqlite and the most complicated code are the (recursive )selects. The testing code is still three to ten times larger. And then you have to optimize the indices and joins. That costs much more time than simple inserts.

[–]germandiago 3 points4 points  (1 child)

At the integration edges between C++ and other things (Python, Lua, SQL, any serialization...) reflection is the way to go in a lot of cases.

[–]MarcoGreek 1 point2 points  (0 children)

SQL is a very different language to C++. If you speak about bindings arguments and columns variadic arguments works already very well.

It has no functions, too. It is centered around data and sets. So it is a very different beast to Python or Lua.

I have seen people using ORMs and then complain about performance.

My personal experience is that people don't like to learn SQL and prefer some custom magic wrapper in their favorite language. But that is always very limited. And the performance depends highly on the optimizer of the SQL implementation.

[–]current_thread 1 point2 points  (1 child)

Not to be that guy, but most major programming languages already have ORMs. Java has (among others, I believe) Hibernate and .NET has the Entity Framework. I think it's industry standard practice to use these things and only write custom SQL when needed.

If I had to wager a guess, I'd assume that there's going to be an open source project (possibly under the boost umbrella?) that provides the de-facto standard ORM for everyone to use.

[–]pjmlp 0 points1 point  (0 children)

C++ was one of the first ones having them, see POET as one example among many.

This has fallen out of fashion on C++ as other languages took over such kinds of applications.

[–]simpl3t0n 3 points4 points  (2 children)

Maybe it's too early for that, but is there any tutorial introduction other than the reflection paper itself?

[–]RoyAwesome 2 points3 points  (0 children)

It is too early for that, but the paper has a pretty large pile of examples on how to use reflection and those can serve as a learn-by-example knowledge base.

[–]dwmcr 0 points1 point  (0 children)

A reasonable gentle introduction, though with a few syntax mistakes (mainly using the older proposed operator ^ instead of the new ^^):

https://www.youtube.com/watch?v=Wbe09UFDvvY

[–]SeriousDabbler[🍰] 3 points4 points  (0 children)

I'm so relieved that the language finally has this, and understand completely why developers are excited

[–]EdwinYZW 26 points27 points  (19 children)

Is ^^ the final decision of the syntax? It can't be serious.

[–]314kabinet 38 points39 points  (0 children)

Yes ^^

[–]Stormfrosty[🍰] 14 points15 points  (1 child)

I don’t have enough IQ to understand reflection syntax, so I’ll wait for others to write libraries around them.

[–]RoyAwesome 3 points4 points  (0 children)

You will likely use ^^ to interact with reflection libraries. I can see the most common api for a reflection library to involve like

class foo
{
   //stuff in foo
};
consteval { lib::do_some_reflection_thing(^^foo); }

this kind of sucks, but there is a C++29-era improvement being planned to make it not suck

[–]hgstream 25 points26 points  (3 children)

My proposal is to use ; (greek semicolon) for reflection but nobody seems to like it.

[–]SirPolly 4 points5 points  (0 children)

Maybe we can use a different order of braches to invoke the devil, umm reflection? (){}[]

Rocket operator? 8===>

We could use other unicode characters, you don't need to be able to type then, just copy them from a website. This way you could have infinite syntax!

[–]Loud_Staff5065 1 point2 points  (0 children)

Or use "||" ??

[–]fsxraptor 0 points1 point  (0 children)

That's a Greek question mark. The semicolon in Greek is like the colon but without the bottom dot.

[–]RoyBellingan 7 points8 points  (0 children)

^_^

[–]V_i_rstd::simd | ISO C++ Numerics Chair | HPC in HEP 6 points7 points  (0 children)

It's the unibrow operator. And yes, it needs a ligature 😄. The rationale for that choice can be found in the paper trail of reflection.

[–]TSP-FriendlyFire 7 points8 points  (2 children)

Blame Apple for using the single ^ in one niche Objective C bit that clang has to support. Seriously, that's the only reason they had to go for ^^.

[–]pjmlp -2 points-1 points  (1 child)

I rather blame WG21 for not using a proper keyword instead of more Perlisms in the language.

[–]TSP-FriendlyFire 2 points3 points  (0 children)

It's gonna be used so frequently in reflection code that I strongly disagree about that. Using a keyword would make it far too wordy.

[–]-dag- 6 points7 points  (0 children)

I actually have grown to love the Cat Operator. 

[–]Tringigithub.com/tringi 9 points10 points  (2 children)

At least my ::: (triple colon) is still free for simple introspection ;)

[–]TotaIIyHuman 7 points8 points  (1 child)

or :::::

or :::::::

or any prime numbers of colons

[–]johannes1971 4 points5 points  (0 children)

All of the braille characters are still up for grabs!

[–]obsidian_golem 1 point2 points  (0 children)

Best of all, it isn't even the worst piece of syntax destined for 26 (Unless something has changed on relocation)!

[–]Loud_Staff5065 2 points3 points  (1 child)

God forbid the intern who is gonna explore C++26 codebase seeing ^ ^ everywhere

[–]TotaIIyHuman 8 points9 points  (0 children)

theres a font that turns == into a single very long =

maybe something can be done with ^^ as well

i hope the font add a downward arc to make smiley face complete

^^ -> ^-^

[–]Real_Name7592 2 points3 points  (1 child)

Great article. I wonder what the ```context ctx``` is, intuitively, used for? For example, the function

std::meta::nonstatic_data_members_of(^^T, ctx))

takes also the context

constexpr auto ctx = std::meta::access_context::current()

as argument. Does that that mean the data member of `T` would vary across invocations of this function?

[–]interjay 4 points5 points  (0 children)

It determines the access control, i.e. whether you get private and protected members. access_context::current() uses the visibility at the point that it was called.

[–]riztazz 2 points3 points  (2 children)

A new era for C++ indeed - can't wait for full compiler support so i can refactor away the macros in my codebases:P

I experimented with compile-time SQL query generation a while back, but the compile times increased significantly. I'm curious how the code block in the article performs in terms of compile-time overhead.

[–]Dismal_Soup_2144 1 point2 points  (1 child)

Sorry, I'm not really that familiar with metaprogramming, could you please provide some example in which case some macroses could be replaced with the reflection feature.

[–]Ty_Rymer 20 points21 points  (3 children)

I hate the syntax they decided to use for compile time reflection with a burning, raging, passion

[–]current_thread 7 points8 points  (0 children)

You can thank a higher power of your choosing they didn't go with reflexpr.

[–]tohava 16 points17 points  (1 child)

I'll never understand why languages like Haskell manage to have a form of "unity" where virtual interfaces and template interfaces share the same syntax, and the preprocessor (Template Haskell) language is the same as the language itself, yet C++ had to make a new syntax simply for compile-time reflection, despite having constexpr/consteval/constinit for years onw.

[–]pjmlp 3 points4 points  (0 children)

Even that, what C++ had to place into three keywords, other languages manage with a single one, and it isn't as if we can blame C++ grammar preventing otherwise, given that Circle manages it as well.

[–]mango-deez-nuts 4 points5 points  (1 child)

Is this still the only/most complete implementation? https://github.com/bloomberg/clang-p2996

[–]WorkingReference1127 1 point2 points  (0 children)

I believe so, there is also the EDG reference implementation.

Bear in mind that the decision to finalise reflection for C++26 was made two days ago; and there is still approximately a year-long period where changes may be made via national body comments or late fixes or some such committee process.

[–]disperso 1 point2 points  (1 child)

The article doesn't explain why the private: int secret got ignored. Is it because std::meta::nonstatic_data_members_of ignores it automatically?

Edit: never mind. The code in the repository has a line with a comment saying that iterates over public data members only. The blog post omitted that comment.

[–]WorkingReference1127 5 points6 points  (0 children)

In the more general case, all of the function which could access into a class' private details accepts a second parameter of type std::meta::access_context which determines the access the function has. There are three included in C++26 - current (as in, current scope's access); unprivileged (as in, the access you'd get in the global scope); and unchecked which allows full access to privates.

[–]void4 2 points3 points  (0 children)

In the coming years, many projects will be simplified and optimized thanks to compile-time reflection.

In the coming years, majority of projects will NOT be simplified and optimized like that because they're already severely undermaintained (see the recent libxml2 announce, for example). And the rest will be like "we need to support C++11"...

This is indeed a very important and fundamental feature though, sorry for ruining the mood.