Use of 'const' for function parameters

前端 未结 30 2712
借酒劲吻你
借酒劲吻你 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:34

    const should have been the default in C++. Like this :

    int i = 5 ; // i is a constant
    
    var int i = 5 ; // i is a real variable
    
    0 讨论(0)
  • 2020-11-22 03:34

    I tend to use const wherever possible. (Or other appropriate keyword for the target language.) I do this purely because it allows the compiler to make extra optimizations that it would not be able to make otherwise. Since I have no idea what these optimizations may be, I always do it, even where it seems silly.

    For all I know, the compiler might very well see a const value parameter, and say, "Hey, this function isn't modifying it anyway, so I can pass by reference and save some clock cycles." I don't think it ever would do such a thing, since it changes the function signature, but it makes the point. Maybe it does some different stack manipulation or something... The point is, I don't know, but I do know trying to be smarter than the compiler only leads to me being shamed.

    C++ has some extra baggage, with the idea of const-correctness, so it becomes even more important.

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

    I use const were I can. Const for parameters means that they should not change their value. This is especially valuable when passing by reference. const for function declares that the function should not change the classes members.

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

    I wouldn't put const on parameters like that - everyone already knows that a boolean (as opposed to a boolean&) is constant, so adding it in will make people think "wait, what?" or even that you're passing the parameter by reference.

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

    As parameters are being passed by value,it doesnt make any difference if you specify const or not from the calling function's perspective.It basically does not make any sense to declare pass by value parameters as const.

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

    Extra Superfluous const are bad from an API stand-point:

    Putting extra superfluous const's in your code for intrinsic type parameters passed by value clutters your API while making no meaningful promise to the caller or API user (it only hampers the implementation).

    Too many 'const' in an API when not needed is like "crying wolf", eventually people will start ignoring 'const' because it's all over the place and means nothing most of the time.

    The "reductio ad absurdum" argument to extra consts in API are good for these first two points would be is if more const parameters are good, then every argument that can have a const on it, SHOULD have a const on it. In fact, if it were truly that good, you'd want const to be the default for parameters and have a keyword like "mutable" only when you want to change the parameter.

    So lets try putting in const whereever we can:

    void mungerum(char * buffer, const char * mask, int count);
    
    void mungerum(char * const buffer, const char * const mask, const int count);
    

    Consider the line of code above. Not only is the declaration more cluttered and longer and harder to read but three of the four 'const' keywords can be safely ignored by the API user. However, the extra use of 'const' has made the second line potentially DANGEROUS!

    Why?

    A quick misread of the first parameter char * const buffer might make you think that it will not modify the memory in data buffer that is passed in -- however, this is not true! Superfluous 'const' can lead to dangerous and incorrect assumptions about your API when scanned or misread quickly.


    Superfluous const are bad from a Code Implementation stand-point as well:

    #if FLEXIBLE_IMPLEMENTATION
           #define SUPERFLUOUS_CONST
    #else
           #define SUPERFLUOUS_CONST             const
    #endif
    
    void bytecopy(char * SUPERFLUOUS_CONST dest,
       const char *source, SUPERFLUOUS_CONST int count);
    

    If FLEXIBLE_IMPLEMENTATION is not true, then the API is “promising” not to implement the function the first way below.

    void bytecopy(char * SUPERFLUOUS_CONST dest,
       const char *source, SUPERFLUOUS_CONST int count)
    {
           // Will break if !FLEXIBLE_IMPLEMENTATION
           while(count--)
           {
                  *dest++=*source++;
           }
    }
    
    void bytecopy(char * SUPERFLUOUS_CONST dest,
       const char *source, SUPERFLUOUS_CONST int count)
    {
           for(int i=0;i<count;i++)
           {
                  dest[i]=source[i];
           }
    }
    

    That’s a very silly promise to make. Why should you make a promise that gives no benefit at all to your caller and only limits your implementation?

    Both of these are perfectly valid implementations of the same function though so all you’ve done is tied one hand behind your back unnecessarily.

    Furthermore, it’s a very shallow promise that is easily (and legally circumvented).

    inline void bytecopyWrapped(char * dest,
       const char *source, int count)
    {
           while(count--)
           {
                  *dest++=*source++;
           }
    }
    void bytecopy(char * SUPERFLUOUS_CONST dest,
       const char *source,SUPERFLUOUS_CONST int count)
    {
        bytecopyWrapped(dest, source, count);
    }
    

    Look, I implemented it that way anyhow even though I promised not to – just using a wrapper function. It’s like when the bad guy promises not to kill someone in a movie and orders his henchman to kill them instead.

    Those superfluous const’s are worth no more than a promise from a movie bad-guy.


    But the ability to lie gets even worse:

    I have been enlightened that you can mismatch const in header (declaration) and code (definition) by using spurious const. The const-happy advocates claim this is a good thing since it lets you put const only in the definition.

    // Example of const only in definition, not declaration
    class foo { void test(int *pi); };
    void foo::test(int * const pi) { }
    

    However, the converse is true... you can put a spurious const only in the declaration and ignore it in the definition. This only makes superfluous const in an API more of a terrible thing and a horrible lie - see this example:

    class foo
    {
        void test(int * const pi);
    };
    
    void foo::test(int *pi) // Look, the const in the definition is so superfluous I can ignore it here
    {
        pi++;  // I promised in my definition I wouldn't modify this
    }
    

    All the superfluous const actually does is make the implementer's code less readable by forcing him to use another local copy or a wrapper function when he wants to change the variable or pass the variable by non-const reference.

    Look at this example. Which is more readable ? Is it obvious that the only reason for the extra variable in the second function is because some API designer threw in a superfluous const ?

    struct llist
    {
        llist * next;
    };
    
    void walkllist(llist *plist)
    {
        llist *pnext;
        while(plist)
        {
            pnext=plist->next;
            walk(plist);
            plist=pnext;    // This line wouldn't compile if plist was const
        }
    }
    
    void walkllist(llist * SUPERFLUOUS_CONST plist)
    {
        llist * pnotconst=plist;
        llist *pnext;
        while(pnotconst)
        {
            pnext=pnotconst->next;
            walk(pnotconst);
            pnotconst=pnext;
        }
    }
    

    Hopefully we've learned something here. Superfluous const is an API-cluttering eyesore, an annoying nag, a shallow and meaningless promise, an unnecessary hindrance, and occasionally leads to very dangerous mistakes.

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