r/GraphicsProgramming • • Aug 14 '22

Finally!!!!! Red Triangle%

Post image
330 Upvotes

27 comments sorted by

View all comments

8

u/314kabinet Aug 14 '22

Congrats! But for god’s sake, use const.

0

u/[deleted] Aug 14 '22

[deleted]

1

u/MausGames Aug 15 '22

Yes, it can affect both code generation and performance.

C++ allows overloading between const and non-const methods: class MyClass { ... void MyMethod(); void MyMethod() const; };

The compiler will select the appropriate overload based on the const-qualifier of the object (if both method-variants are present).

There are also more obscure cases, from my experience.

If you declare a global static-const variable, most compilers will place it into read-only memory, and trying to modify this variable (with a const_cast or indirect addressing) will cause an access-violation. It also allows the compiler to inline the variable in the whole translation-unit, because it knows the value cannot be modified.

That second example is quite vague of course, but it shows that the compiler cares about const-qualification in unexpected ways.