"my_string"_hs is the same as entt::hashed_string( "my_string" ), however the hashed_string class contains both compile-time and run-time storage, with a constexpr typecast operator to entt::id_type (a uint32_t) which returns the hashed value. This allows hashed string literals to be used as compile time arguments for anything which expects such an integral parameter, without needing a whole bunch of extra syntax.
For example, EnTT supports "tag" components inside its ECS registry. entt::tag<T> is simply an empty struct where T is in integral parameter, so if you wanna add a named tag to an entity you can write
True, most of the languages that are touted as weird, unreadable, difficult, etc. by the mainstream are usually simple and force much much less congnitive load on you than C++. Haskell might be closer to C in difficulty than C++.
I could see the use case of a generic class which returns the nullptr when cast to a pointer, so that compiler errors have the names of the type(s) you have attached to the error message, but I am pretty sure that OP's code doesn't do that.
It indeed does not do that. I just wanted a way to write a "comment" inside a nullptr argument. Some sort of way to self document why the nullptr is there :)
So you implemented an overload that lets you take a string literal, just to ignore it? The title ("named nullptr") might hint at the fact that this null now has a name, but afaik (not the best C++ Dev though) that at most helps while reading the code, but for this you could also use the variable name or a comment and it would have the same result (except being easier).
100
u/Nuclear_creeperMCBE 4d ago
C++ is a crazy language. Can anyone explain why you would ever want to overload what appears to be suffixed string literals.
3
u/Ben-Goldberg 4d ago
I could see the use case of a generic class which returns the nullptr when cast to a pointer, so that compiler errors have the names of the type(s) you have attached to the error message, but I am pretty sure that OP's code doesn't do that.
198
u/KGBsurveillancevan 4d ago
Every time I see C++ code posted here I see some syntax I’ve never seen before
52
u/No_Belt_9829 4d ago
Absolute horrid, darling
25
u/W00GA 4d ago
i didn't know "fucked up shit" was a language
13
u/Sacaldur 4d ago
So you implemented an overload that lets you take a string literal, just to ignore it? The title ("named nullptr") might hint at the fact that this null now has a name, but afaik (not the best C++ Dev though) that at most helps while reading the code, but for this you could also use the variable name or a comment and it would have the same result (except being easier).
5
u/AmazedStardust 3d ago
Does this just let you type "placeHolder" instead of "nullptr"?
2
u/GoddammitDontShootMe [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” 3d ago
I have no clue what possible purpose this could serve.
1
u/blockMath_2048 3d ago
wait wtf that’s a thing?????
1
u/Antagonin 3d ago
Why not use comments?
1
u/uvero 22h ago
All C++ makes me smell toast tbfh
1
u/NamedBird 3d ago
In C, name one of your variables "nullptr", just because you can.
Then when a C++ dev tries to use your code, you can watch the resulting fire... 🤣