Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Make 'Shader::use()' const as well #203

wants to merge 1 commit into from

Conversation

amengol
Copy link

@amengol amengol commented Oct 8, 2020

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?

@wmcnamara
Copy link

Even though this is old, +1.

Const correctness is important.

@JG-Adams
Copy link

Even though this is old, +1.

Const correctness is important.

Howdy! Can you explain why it's important?
Also, the Skeleton Animation library have lots of getters that can have const as well!

@wmcnamara
Copy link

Even though this is old, +1.
Const correctness is important.

Howdy! Can you explain why it's important? Also, the Skeleton Animation library have lots of getters that can have const as well!

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:

const Shader shader("PathToShader");

Because Shader::Use() is not marked as const, you cannot call shader.Use() on the object. Shader::Use doesnt modify any member variables, so it makes no sense for it to be non const, and because of this you cant use the shader class correctly.

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

@JG-Adams
Copy link

JG-Adams commented Aug 2, 2022

Even though this is old, +1.
Const correctness is important.

Howdy! Can you explain why it's important? Also, the Skeleton Animation library have lots of getters that can have const as well!

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:

const Shader shader("PathToShader");

Because Shader::Use() is not marked as const, you cannot call shader.Use() on the object. Shader::Use doesnt modify any member variables, so it makes no sense for it to be non const, and because of this you cant use the shader class correctly.

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!
That is very handy. I wonder why it had be unintuitive? Who would have thought of it?

@wmcnamara
Copy link

Even though this is old, +1.
Const correctness is important.

Howdy! Can you explain why it's important? Also, the Skeleton Animation library have lots of getters that can have const as well!

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:
const Shader shader("PathToShader");
Because Shader::Use() is not marked as const, you cannot call shader.Use() on the object. Shader::Use doesnt modify any member variables, so it makes no sense for it to be non const, and because of this you cant use the shader class correctly.
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! That is very handy. I wonder why it had be unintuitive? Who would have thought of it?

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.

@JG-Adams
Copy link

JG-Adams commented Aug 2, 2022

Even though this is old, +1.
Const correctness is important.

Howdy! Can you explain why it's important? Also, the Skeleton Animation library have lots of getters that can have const as well!

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:
const Shader shader("PathToShader");
Because Shader::Use() is not marked as const, you cannot call shader.Use() on the object. Shader::Use doesnt modify any member variables, so it makes no sense for it to be non const, and because of this you cant use the shader class correctly.
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! That is very handy. I wonder why it had be unintuitive? Who would have thought of it?

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 like C++ because I've used Gamemaker 8.0 for decade and wasn't happy with it's performance.
I was looking for a language that work for all needs. And it does appear that you can definitely turn a complicated language into a much simpler one! C++ Allows you to do that. I love that.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants