Use of 'const' for function parameters

前端 未结 30 2797
借酒劲吻你
借酒劲吻你 2020-11-22 03:06

How far do you go with const? Do you just make functions const when necessary or do you go the whole hog and use it everywhere? For example, imag

相关标签:
30条回答
  • 2020-11-22 03:24

    May be this wont be a valid argument. but if we increment the value of a const variable inside a function compiler will give us an error: "error: increment of read-only parameter". so that means we can use const key word as a way to prevent accidentally modifying our variables inside functions(which we are not supposed to/read-only). so if we accidentally did it at the compile time compiler will let us know that. this is specially important if you are not the only one who is working on this project.

    0 讨论(0)
  • 2020-11-22 03:26

    The reason is that const for the parameter only applies locally within the function, since it is working on a copy of the data. This means the function signature is really the same anyways. It's probably bad style to do this a lot though.

    I personally tend to not use const except for reference and pointer parameters. For copied objects it doesn't really matter, although it can be safer as it signals intent within the function. It's really a judgement call. I do tend to use const_iterator though when looping on something and I don't intend on modifying it, so I guess to each his own, as long as const correctness for reference types is rigorously maintained.

    0 讨论(0)
  • 2020-11-22 03:27

    Marking value parameters 'const' is definitely a subjective thing.

    However I actually prefer to mark value parameters const, just like in your example.

    void func(const int n, const long l) { /* ... */ }
    

    The value to me is in clearly indicating that the function parameter values are never changed by the function. They will have the same value at the beginning as at the end. For me, it is part of keeping to a very functional programming sort of style.

    For a short function, it's arguably a waste of time/space to have the 'const' there, since it's usually pretty obvious that the arguments aren't modified by the function.

    However for a larger function, its a form of implementation documentation, and it is enforced by the compiler.

    I can be sure if I make some computation with 'n' and 'l', I can refactor/move that computation without fear of getting a different result because I missed a place where one or both is changed.

    Since it is an implementation detail, you don't need to declare the value parameters const in the header, just like you don't need to declare the function parameters with the same names as the implementation uses.

    0 讨论(0)
  • 2020-11-22 03:28

    In the case you mention, it doesn't affect callers of your API, which is why it's not commonly done (and isn't necessary in the header). It only affects the implementation of your function.

    It's not particularly a bad thing to do, but the benefits aren't that great given that it doesn't affect your API, and it adds typing, so it's not usually done.

    0 讨论(0)
  • 2020-11-22 03:30

    All the consts in your examples have no purpose. C++ is pass-by-value by default, so the function gets copies of those ints and booleans. Even if the function does modify them, the caller's copy is not affected.

    So I'd avoid extra consts because

    • They're redudant
    • They clutter up the text
    • They prevent me from changing the passed in value in cases where it might be useful or efficient.
    0 讨论(0)
  • 2020-11-22 03:32

    const is pointless when the argument is passed by value since you will not be modifying the caller's object.

    const should be preferred when passing by reference, unless the purpose of the function is to modify the passed value.

    Finally, a function which does not modify current object (this) can, and probably should be declared const. An example is below:

    int SomeClass::GetValue() const {return m_internalValue;}
    

    This is a promise to not modify the object to which this call is applied. In other words, you can call:

    const SomeClass* pSomeClass;
    pSomeClass->GetValue();
    

    If the function was not const, this would result in a compiler warning.

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