-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make 'Shader::use()' const as well #203
base: master
Are you sure you want to change the base?
Conversation
Even though this is old, +1. Const correctness is important. |
Howdy! Can you explain why it's important? |
Const correctness is super important because it allows you to use the objects with const properly, makes the code more consistent and predictable, and allows the compiler to make numerous optimisations. For example, if you made a const shader object such as:
Because Because of this, most people instead of marking the function as const, simply make the object non const, which can make finding changes and predicting code harder, and inhibit numerous compiler optimisations. Its also really important when taking constant values/refs: Consider the following function: void SetupShader(const Shader& shader)
{
//Sets up a bunch of shader stuff
shader.Use(); //DOESNT COMPILE, even though I should be able to call Use on a const shader.
} I hope this helps address it! You may also be interested in this: https://isocpp.org/wiki/faq/const-correctness |
Thank you for taking the time to explain all that in fine details! |
A lot of things in C++ just tend to be unintuitive, kind of just one of these things with the language unfortunately hahah. Maybe in theory they could have some sort of system that automatically checks if the function modifies members, then makes it const/non const based on that, but that might frustrate some people, and some people might still want to make functions that dont modify member variables nonconst in some situations. (Mostly in situations where it doesnt change member variables but modifies global data or something, and they cant protect the global data with a mutex for whatever reason, and want to make it clear that its not thread safe) Writing this has made me realize how complicated C++ is in general hahaha. |
Lol yeah! It's pretty amazing though! I think what can be done to simplify the language is to have someone who can with great clarity, clarify the complicated stuff in the simplest way possible. Using the most up to date simplest method that work in a lot of conditions. Like smart pointer did. And how to use it well. |
I believe that function can be const as well. Compiled all the projects and there was no error after the change.
Was there a specific reason to not be const?