r/programminghorror • • 4d ago

c++ Named nullptrs

Post image
372 Upvotes

43 comments sorted by

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.

33

u/Avelina9X 4d ago

EnTT uses it for hashed strings!

"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

myRegistry.emplace<entt::tag<"my_tag"_hs>>( myEntity );

And then later on if you want to create a view over components like entity positions or rotations or whatever if and only if they have that tag.

auto view = myRegistry.view<Position, Rotation, entt::tag<"my_tag"_hs>>()

13

u/Most-Bookkeeper-950 3d ago

this person appears to be cracked

8

u/Suitable_Plate4943 3d ago

the whole entt codebase has some of the nerdiest C++ ive ever seen to this day
bro has a nice blog too

8

u/Nuclear_creeperMCBE 4d ago

This is genuinely insane in the best way possible. Might start learning c++ just for this wierd features

4

u/Axman6 3d ago

People say Haskell is weird, and then go and choose to program in C++. I’ll never understand that, and I like writing the sort of code C++ is best at. 

3

u/Valuable_Leopard_799 3d ago

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++.

2

u/AnArmoredPony 3d ago

I wish there was a concept (or two) that would generalize this feature

1

u/2435191 2d ago

curious how Unix pipelines generalize this

1

u/AnArmoredPony 2d ago

"string" | hash

1

u/Ratstail91 2d ago

EnTT is kinda neat.

89

u/FloweyTheFlower420 4d ago

Out of all the horrible C++ features, user-defined literal suffixes is not one of them.

34

u/Nuclear_creeperMCBE 4d ago

Never said it was a bad feature just a bizzare addition. Like is typing "foo"custom_string that much better then custom_string("foo")

38

u/FloweyTheFlower420 4d ago

Yes, I'd much rather "xxx"_sv than invoke the string_view constructor. Less clutter. Also helpful for integer literal suffix (for units).

13

u/Mrblahblah200 3d ago

Yeah it actually is

-7

u/TOMZ_EXTRA 4d ago

It seems like a worse version of Rust traits which can achieve a very similar result (for example 5.ms())

21

u/sr9zzek2y 4d ago

std::chrono defines custom suffix operators for it's durations, so if you want to check if a duration is over 5ms, you do duration > 5ms

3

u/Rararror 3d ago

This is nice

7

u/Qwexet 4d ago

One usecase is if you write your own string class. You can also make user defined literals with numbers

6

u/Pie-Lang 4d ago

“Name”_human
vs
Human{“Name”}

pick your references.

5

u/Nuclear_creeperMCBE 4d ago

Is there a semantic difference or is it purely syntactic

6

u/_Ilobilo_ 4d ago

std::string_view { "hello" } vs "hello"sv

2

u/S4N7R0 4d ago

Commitee likes converting string literals to string_view wit it https://en.cppreference.com/cpp/string/basic_string_view/operator%22%22sv

1

u/ice456cream 4d ago

I mean js has tagged string literals tag`value` which runs tag as a template

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.

3

u/Avelina9X 3d ago

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 :)

1

u/Ben-Goldberg 3d ago

Like writing assert("comment")?

198

u/KGBsurveillancevan 4d ago

Every time I see C++ code posted here I see some syntax I’ve never seen before

44

u/johntheswan 4d ago

c++ contains multitudes

44

u/Axman6 3d ago

C++ syntax is fractal, everywhere there’s syntax, more syntax gets added. For example:  https://youtu.be/EqiLTgQcDPM

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... 🤣

2

u/Wenir 3d ago

just because you can

You can't 

3

u/NamedBird 3d ago

you can though?
It's a valid string to use as variable.

"nullptr" is only a reserved keyword in the newest unstable C23 version...
https://en.cppreference.com/c/language/nullptr

4

u/Wenir 3d ago

unstable

Anything released in this millennium is too old?

2

u/NamedBird 3d ago

We're in r/programminghorror, anything released yesterday is considered unstable...