this post was submitted on 10 Feb 2024
19 points (82.8% liked)

C++

1704 readers
1 users here now

The center for all discussion and news regarding C++.

Rules

founded 1 year ago
MODERATORS
 

I did a code that uses custom literals to allow indexing arrays with ordinals (1st, 2nd, 3rd), all at compile time. Additionally _st works only with 1, _nd only with 2, _rd only with 3. Zero and negative numbers are also not supported and fail compilation.

https://github.com/serpent7776/bits/blob/master/th.cpp/th.cpp

all 12 comments
sorted by: hot top controversial new old
[–] [email protected] 9 points 6 months ago (2 children)

What's the purpose of this? If you want to access an index in an array, isn't using integers easier?

And if you want string literals, you can make a map and access the key that way as well

[–] [email protected] 15 points 6 months ago (2 children)

Does it matter? OP had an itch. He scratched it, and shared it with the world.

The reason could be "I wonder if I can?". That's good enough.

[–] [email protected] 10 points 6 months ago

Yeah, that was basically it.

[–] [email protected] 1 points 6 months ago (1 children)

"Does it matter?"

That was the point of my question, thanks for reiterating it

[–] [email protected] 1 points 6 months ago (1 children)

The point of your question seems to be “never try things a for fun”

Curmudgeonry sucks all the joy out of anything.

[–] [email protected] 3 points 6 months ago

I never said that, please don't assume that's what I meant before asking.

[–] [email protected] 14 points 6 months ago (1 children)

There's no purpose really. It was more of "wonder if I can" and also nice way to learn custom literals.

[–] [email protected] 2 points 6 months ago

Yeah it's always a good way to learn things.

I only asked because I was genuinely curious if there was something I was missing about why you created this, that's all.

[–] [email protected] 2 points 6 months ago (1 children)

This sounds pretty interesting to do for funsies. I wonder if / how easy can it be extended to support i18n (eg.: "1_ro", "2_do", "3_ro", "4_to" for Spanish).

Also thanks for not supporting negatvie integers. Trying to do that with UDLs over integers leads to rather annoying "funsies", see StackOverflow and glados-418.

[–] [email protected] 2 points 6 months ago (1 children)

It should be easy to edit the existing code to add support for i18n, but there's probably no way to make in generic (except for macros).

[–] [email protected] 2 points 6 months ago

Sounds like a plan for a funsies long weekend, might send a PR if I advance with it enough.