Const correctness for value parameters

前端 未结 6 810
野性不改
野性不改 2020-11-28 12:09

I know there are few question about const correctness where it is stated that the declaration of a function and its definition do not need to agree for value parameters. Thi

相关标签:
6条回答
  • 2020-11-28 12:20

    My take on it:

    It's not a bad idea, but the issue is minor and your energy might be better spent on other things.

    In your question you provided a good example of when it might catch an error, but occasionally you also end up doing something like this:

    void foo(const int count ...)
    {
       int temp = count;  // can't modify count, so we need a copy of it
       ++temp;
    
       /* ... */
    }
    

    The pros and cons are minor either way.

    0 讨论(0)
  • 2020-11-28 12:33

    If there is const keyword present; it means value of 'i' (which is const type) can not be modified. If value of 'i' is changed inside foo function compiler will throw error: "

    Can not modify const object

    But changing '*i' (i.e. *i = 3;)means you are not changing value of 'i' but value of address pointed by 'i'

    Actually,the const function is appropriate for large objects that should not be altered by function.

    0 讨论(0)
  • 2020-11-28 12:34

    I like const correctness for situations like this:
    void foo(const Bar &b) //I know b cannot be changed
    {
    //do something with b
    }

    This lets me use b without fear of modifying it, but I don't have to pay the cost of a copy constructor.

    0 讨论(0)
  • 2020-11-28 12:35

    I've read many times that making value parameters in a function const is a bad thing to do because it's unnecessary.

    However, I find it occasionally helpful to me as a check that my implementation doesn't do something I don't intend (as in the example at the end of your question).

    So, while it may not add value to the caller, it does sometimes add a small bit of value to me as an implementer, and it doesn't take anything away from the caller. So I see no harm using it.

    For example, I may be implementing a C function that takes a couple pointers to a buffer - a pointer to the start, and a pointer to the end. I'm going to put data in the buffer, but want to ensure that I don't overrun the end. So inside the function there's code that will increment a pointer as I'm adding data to it. Making the pointer to the end of the buffer a const parameter will ensure that I don't code up a bug that accidentally increments the end boundary pointer instead of the pointer I really should be incrementing.

    So a fillArray function with a signature like this:

    size_t fillArray( data_t* pStart, data_t* const pEnd);
    

    will prevent me from accidentally incrementing pEnd when I really mean to increment pStart. It's not a huge thing, but I'm pretty sure everyone who has programmed for any period of time in C has run across such a bug.

    0 讨论(0)
  • 2020-11-28 12:38

    Unfortunately, some compilers (I'm looking at you, Sun CC!) incorrectly differentiate between arguments declared const and ones not declared so, and you can get errors about undefined functions.

    0 讨论(0)
  • I think this is dependent on your personal style.

    It doesn't add or subtract to what clients can pass to your function. In essence it's like a compile-time assertion. If it helps you to know that value won't change, go ahead and do it, but I don't see a big reason for others to do it.

    One reason I might not do it is that the const-ness of the value parameter is an implementation detail that your clients don't need to know about. If you later (purposely) change your function so that it actually does change that value, you will need to change the signature of your function, which will force your clients to re-compile.

    This is similar to why some people recommend having no public virtual methods (the functions virtual-ness is an implementation detail that should be hidden from clients), but I'm not in that particular camp.

    0 讨论(0)
提交回复
热议问题