Thread: Is there any real reason to use "const"

  1. #1
    Registered User
    Join Date
    Oct 2021
    Posts
    138

    Is there any real reason to use "const"

    Rather than just tell the compiler to not allow me to modify a variable (like I don't know what I'm doing with my program), are there any reason to use "const" variables?

    Other than out of curiosity, I'm actually asking because I'm writing a transpiler and I want to know if I should support "const" (or the concept of immutability in general) or not. To me, "const" has a lot of burdens like:

    "should we use it all the time if we don't want to be sure that we won't modify a variable or only for critical ones?"

    "Then in this case, why not make "const" the default and use another word to allow a variable to be mutable (just like Rust and other languages)?"

    Also another problem is that I don't like the way "const" is treated in functions. For example, you cannot past "const" variables to functions that take non-const parameters even if we are using variables which are ALWAYS copied (which means than modifying them will not modify the original value). This is stupid because if I want to support "const" for a function, I need to make an extra instruction to copy the value to a new non-const variable that is created inside the function and make an extra instruction. Copying a value is not a slow operation but it can be for a big struct. And in any case, it is stupid and it ........es me off....

    So yeah, I would like to know as soon as possible if there are any real reasons to support something like that so I can implement it.

  2. #2
    Registered User
    Join Date
    Sep 2020
    Posts
    150
    Copying a value is not a slow operation but it can be for a big struct.
    Why would you want to copy a struct ? Pass it as const pointer or const ref.

  3. #3
    Registered User
    Join Date
    Oct 2021
    Posts
    138
    Quote Originally Posted by thmm View Post
    Why would you want to copy a struct ? Pass it as const pointer or const ref.
    I don't mean that. But still, it turns out that I'm a complete idiot because I thought that you could not pass "const" variables in "non-const" parameters even when they were passed by copy. I thought I tried it out some at some point and it didn't worked.... Or does it truly not work in specific scenarios? Anyway this is what I thought wasn't possible to do but it is completely possible.

    Code:
    Code:
    #include   typedef struct {   int val; } test;
    
    void print_and_change_val(test value) {
        value.val = 100;
        printf("The modified value is: %d\n", value.val);
    }
    
    int main() {
        const test t = { 200 };
        printf("The value inside 'main' is: %d\n", t.val);
        print_and_change_val(t);
    }
    Aaaaaaaaaaaaahhhhh!!!! Why am I so stupid and lazy????? Mostly the last one....

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by rempas
    like I don't know what I'm doing with my program
    Yes, you don't know what you're doing with your program. If you disagree, read your code from decades ago and you'll realise that it is true
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Oct 2021
    Posts
    138
    Quote Originally Posted by laserlight View Post
    Yes, you don't know what you're doing with your program. If you disagree, read your code from decades ago and you'll realise that it is true
    Yeah, Tbh I though about it again and I probably will support "const". Tbh, it's the same way with memory management. People forget, do mistakes, program late at night etc. So I suppose it's worth it!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 02-20-2017, 03:53 PM
  2. expected ";" before "const" error.
    By Aenn in forum C++ Programming
    Replies: 0
    Last Post: 01-18-2015, 11:08 AM
  3. my malloc is stuck on "8" for some reason... why?
    By c_seeker in forum C Programming
    Replies: 4
    Last Post: 01-08-2014, 03:19 AM
  4. Replies: 3
    Last Post: 11-15-2009, 04:57 AM
  5. Replies: 17
    Last Post: 12-15-2006, 11:02 AM

Tags for this Thread