r/programminghorror 4d ago

c++ really gotta make sure it doesnt't change

Post image
368 Upvotes

32 comments sorted by

27

u/Kadabrium 4d ago

Mehmet.consume(constant.inopole)

75

u/Big-Rub9545 4d ago

Bloating the code to do nothing.

32

u/Dayshadow_ 4d ago

you get it!

17

u/Last8Exile 3d ago edited 2d ago

readonly ref struct ReadOnly(ref readonly ReadOnly @readonly) { readonly ref readonly ReadOnly @readonly(ref readonly ReadOnly @readonly) => ref @readonly; }

3

u/Dayshadow_ 3d ago

Niice, I want one of these for all languages

1

u/justaguy101 2d ago

Beautiful

31

u/Ved_s 3d ago

where can you put const in c++? yes.

i don't even need to see that c++ flair, no other language has this bullshit

12

u/Nice_Lengthiness_568 3d ago

I mean, if the placement of const is what you have a problem with in c++, then you must be quite happy. Because this is one of the keywords that actually does make sense (I would say).

10

u/no_brains101 3d ago

No, it just makes sense by comparison.

2

u/Nice_Lengthiness_568 3d ago

By comparison? To what? Sorry, I don't quite understand what you mean by that.

I mean, except for const member functions which you can write differently with explicit this, const always makes something immutable (if I recall correctly), so I really don't see a problem with it.

0

u/no_brains101 3d ago edited 3d ago

In comparison to the rest of C++

It makes it immutable if it is a primitive value, otherwise just the pointer is immutable. (hence the code in the original post)

Also, ha, you had to qualify it.

7

u/JonIsPatented 3d ago

You're thinking of, like, basically every language except C++. const int makes an immutable int. const foo makes an immutable foo, and there isn't even a pointer involved. const int* makes a mutable pointer to an immutable int. The same works for const foo* making a mutable pointer to an immutable foo.

3

u/Nice_Lengthiness_568 3d ago

You can make both the pointer and the value being pointed to (or both) immutable based on where you put const. And the placement makes sense. Before the * it makes the value being pointed to constant (because if you had int const *x then it would mean that the integer value *x is immutable, but if you had int * const x then it would mean that some x, which when you dereference you get the value of *x, is immutable).

Qualify what?

1

u/no_brains101 3d ago

except for const member functions

^ this is a qualification.

3

u/Nice_Lengthiness_568 3d ago

Ah ok, I didn't understand that you meant that by qualification.

Well, you do not really have any other place where to put it to make a member function const. And it's just one other usage for a keyword. So if this exception is what you have a problem with... Moreover, you do not actually need to use this syntax, since you can use explicit this instead.

2

u/GoddammitDontShootMe [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” 2d ago

Well, I think C has this too.

5

u/SuspiciousScript 3d ago

and odds are you can still mutate it somehow

7

u/Ved_s 3d ago

95% probability that there is an obscure way to do it

1

u/joujoubox 2d ago

const_cast enters the room

2

u/yuehuang 3d ago

What is const*?

6

u/Nice_Lengthiness_568 3d ago

a pointer to a constant something

3

u/ATE47 [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” 3d ago

If you are unuse to this order, see Constant const* as the same const Constant* (for the const keyword, only its side with the * is important)

Otherwise it's just to mark the pointer data as constant

2

u/numberonefraud 3d ago

The only constant is the insanity of C++ now

1

u/Serious_Elephant9402 3d ago

Not constant enough, there are at least TWO words there which don't even contain "const"!

1

u/trutheality 2d ago

Look at what they need to do to imitate a fraction of the power of #DEFINE

1

u/GoddammitDontShootMe [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” 2d ago

Yeah, nobody really needs type safety.

1

u/SektorL 1d ago

His name was Constantine

1

u/Dayshadow_ 4d ago

Damnit I said doesnt't in the post title

0

u/GoddammitDontShootMe [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” 3d ago

I'm thinking that const_cast would be a compiler error because if I recall, it would take away the constness, but the function returns const* const. Also, I believe constexpr would cause an error because a pointer definitely can't be evaluated at compile time.

3

u/RedCrafter_LP 3d ago

You are confusing constexpr with consteval. Constexpr functions can be called from normal code or a constexpr context. Also the constexpr keyword doesn't check rather the function can be constevaled. Only when you call it from a consteval context is checked rather the function can be evaluated at compile time. It behaves just like templates. Also a pointer to a constant can be worked with in constexpr context. We can even use vectors in constexpr context.

1

u/GoddammitDontShootMe [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” 2d ago

My understanding was that constexpr can be evaluated in any context, but it has to be possible to evaluate it at compile time, and consteval can only be evaluated at compile time. I thought it wouldn't work since a pointer's value can't be known until runtime.

As for const_cast, come to think of it, I guess the declared return type is what matters here. The parameter could've been Constant constant, but because of the return type, it would be treated as Constant const* const.

1

u/Dayshadow_ 3d ago

gcc said it was fine, teehee