const correctness

Discussion in 'C++' started by mandydhaliwal@gmail.com, Oct 13, 2005.

  1. Guest

    Hi Guys,


    I was going through a product's source code. They never use const
    function arguments with default value.
    for instance

    Never noticed following type of declaration

    foo ( const int param1, const bool = false) const;

    instead
    of this
    foo ( const int param1, bool = false) const; is used widely.

    I there any drawback of using default argument with const keyword?
     
    , Oct 13, 2005
    #1
    1. Advertising

  2. * :
    >
    > I was going through a product's source code. They never use const
    > function arguments with default value.
    > for instance
    >
    > Never noticed following type of declaration
    >
    > foo ( const int param1, const bool = false) const;
    >
    > instead
    > of this
    > foo ( const int param1, bool = false) const; is used widely.
    >
    > I there any drawback of using default argument with const keyword?


    No, the two declarations are equivalent (and by that I mean, they would
    specify exactly the same function signature, if a return type was added).

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Oct 13, 2005
    #2
    1. Advertising

  3. David White Guest

    wrote:
    > Hi Guys,
    >
    >
    > I was going through a product's source code. They never use const
    > function arguments with default value.
    > for instance
    >
    > Never noticed following type of declaration
    >
    > foo ( const int param1, const bool = false) const;
    >
    > instead
    > of this
    > foo ( const int param1, bool = false) const; is used widely.
    >
    > I there any drawback of using default argument with const keyword?


    I don't think there's any point in making any pass-by-value parameter const,
    default or not. The const only affects how the function can use the
    parameter and makes no difference to the caller. Why should the public
    interface declare that a function will not modify its own private copy of an
    argument? IMO such consts clutter up the public interface with junk that is
    really none of the caller's business.

    DW
     
    David White, Oct 13, 2005
    #3
  4. David White wrote:
    > wrote:
    > > Hi Guys,
    > >
    > >
    > > I was going through a product's source code. They never use const
    > > function arguments with default value.
    > > for instance
    > >
    > > Never noticed following type of declaration
    > >
    > > foo ( const int param1, const bool = false) const;
    > >
    > > instead
    > > of this
    > > foo ( const int param1, bool = false) const; is used widely.
    > >
    > > I there any drawback of using default argument with const keyword?

    >
    > I don't think there's any point in making any pass-by-value parameter const,
    > default or not. The const only affects how the function can use the
    > parameter and makes no difference to the caller. Why should the public
    > interface declare that a function will not modify its own private copy of an
    > argument? IMO such consts clutter up the public interface with junk that is
    > really none of the caller's business.
    >


    it's makes difference when your const object behave not like no-const:

    struct test {
    void operator () () { }
    void operator () () const { /* terrible things here */ }
    };
     
    Aleksey Loginov, Oct 13, 2005
    #4
  5. "David White" <> wrote in message
    news:...
    > "Aleksey Loginov" <> wrote in message
    > >
    > > it's makes difference when your const object behave not like no-const:
    > >
    > > struct test {
    > > void operator () () { }
    > > void operator () () const { /* terrible things here */ }
    > > };

    >
    > I don't see how that relates to my post. If a function declares a
    > pass-by-value parameter const, you are free to pass either a const or
    > non-const value to it, since it's only the copy received by the function,
    > not the original, that is declared const.


    void foo( test t )
    {
    t();
    }

    void foo( test const t )
    {
    t(); /* those terrible things now happen */
    }
     
    Risto Lankinen, Oct 13, 2005
    #5
  6. David White Guest

    "Aleksey Loginov" <> wrote in message
    news:...
    > David White wrote:
    > > I don't think there's any point in making any pass-by-value parameter

    const,
    > > default or not. The const only affects how the function can use the
    > > parameter and makes no difference to the caller. Why should the public
    > > interface declare that a function will not modify its own private copy

    of an
    > > argument? IMO such consts clutter up the public interface with junk that

    is
    > > really none of the caller's business.
    > >

    >
    > it's makes difference when your const object behave not like no-const:
    >
    > struct test {
    > void operator () () { }
    > void operator () () const { /* terrible things here */ }
    > };


    I don't see how that relates to my post. If a function declares a
    pass-by-value parameter const, you are free to pass either a const or
    non-const value to it, since it's only the copy received by the function,
    not the original, that is declared const.

    DW
     
    David White, Oct 13, 2005
    #6
  7. David White Guest

    "Risto Lankinen" <> wrote in message
    news:y%p3f.11536$...
    >
    > "David White" <> wrote in message
    > news:...
    > > "Aleksey Loginov" <> wrote in message
    > > >
    > > > it's makes difference when your const object behave not like no-const:
    > > >
    > > > struct test {
    > > > void operator () () { }
    > > > void operator () () const { /* terrible things here */ }
    > > > };

    > >
    > > I don't see how that relates to my post. If a function declares a
    > > pass-by-value parameter const, you are free to pass either a const or
    > > non-const value to it, since it's only the copy received by the

    function,
    > > not the original, that is declared const.

    >
    > void foo( test t )
    > {
    > t();
    > }
    >
    > void foo( test const t )
    > {
    > t(); /* those terrible things now happen */
    > }


    Well, that looks like a completely contrived and unrealistic example to me.
    The only times I've ever had const and non-const versions of the same
    function are when they do exactly the same thing with the exception of
    const, e.g.,

    class Point
    {
    public:
    const double &X() const { return x; }
    double &X() { return x; }
    // ...
    private:
    double x;
    // ...
    };

    In any case, you still don't have to make your parameter const:
    void foo( test t )
    {
    const test &u = t;
    u();
    }

    I maintain that what goes on inside foo is of no concern to the caller, and
    so should not manifest itself in the declaration of the parameter the caller
    sees. After all, the parameter will cease to exist once the function
    returns.

    DW
     
    David White, Oct 13, 2005
    #7
  8. mlimber Guest

    David White wrote:
    [snip]
    > I maintain that what goes on inside foo is of no concern to the caller, and
    > so should not manifest itself in the declaration of the parameter the caller
    > sees. After all, the parameter will cease to exist once the function
    > returns.


    I agree with your point about cluttering up the interface, but I do
    think that const pass-by-value parameters are useful. First, note that
    one needn't put the const in the function prototype or class
    declaration in order to use it in the definition since, as Alf notes
    above, the const doesn't change the function signature:

    struct Foo { Foo( int ); };

    Foo::Foo( const int param )
    {
    const int magicNo = 42;
    // ...
    }

    Applying const to param in the definition is no different than applying
    it to the local constant magicNo, but making them both const makes the
    function "easier to understand, track, and reason about" (_C++ Coding
    Standards_, p. 30) because the programmer's intention is clear -- viz.,
    neither magicNo or param will change throughout the function.

    Of course there are cases where one might not want to make all
    pass-by-value parameters const, but the general principle of
    const-correctness is to make everything const that can be const. I
    would apply that to pass-by-value parameters as well.

    Cheers! --M
     
    mlimber, Oct 13, 2005
    #8
  9. Greg Comeau Guest

    In article <>,
    <> wrote:
    >I was going through a product's source code. They never use const
    >function arguments with default value.
    >for instance
    >
    >Never noticed following type of declaration
    >
    >foo ( const int param1, const bool = false) const;
    >
    >instead
    >of this
    >foo ( const int param1, bool = false) const; is used widely.
    >
    >I there any drawback of using default argument with const keyword?


    In and of itself, that should be neutral.

    You will find shops/people who don't allow const at the
    top level in general, as they take the signature, and hence
    interface of the functions very seriously, and so feel that
    the const has no place at the top level. So one issue is
    philosphical/conceptual lieing ;) But it can lead to physical
    confusion, for instance:

    void foo(const int arg);

    Turns out that the const is tossed in the example,
    since it is only a declaration. However, it would not
    be tossed in the definition:

    void foo(const int arg) { /* ... */ }

    hence coming full circle to the opponents of such use.

    Personally I don't like having to declare something only
    then to make a const copy of it, or just ignore it (leaving
    it non-const), but the above make sense too.
    --
    Greg Comeau / Celebrating 20 years of Comeauity!
    Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
    World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
    Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
     
    Greg Comeau, Oct 13, 2005
    #9
  10. Greg Comeau Guest

    In article <>,
    Aleksey Loginov <> wrote:
    >David White wrote:
    >> wrote:
    >> > I was going through a product's source code. They never use const
    >> > function arguments with default value.
    >> > for instance
    >> >
    >> > Never noticed following type of declaration
    >> >
    >> > foo ( const int param1, const bool = false) const;
    >> >
    >> > instead
    >> > of this
    >> > foo ( const int param1, bool = false) const; is used widely.
    >> >
    >> > I there any drawback of using default argument with const keyword?

    >>
    >> I don't think there's any point in making any pass-by-value parameter const,
    >> default or not. The const only affects how the function can use the
    >> parameter and makes no difference to the caller. Why should the public
    >> interface declare that a function will not modify its own private copy of an
    >> argument? IMO such consts clutter up the public interface with junk that is
    >> really none of the caller's business.

    >
    >it's makes difference when your const object behave not like no-const:
    >
    >struct test {
    > void operator () () { }
    > void operator () () const { /* terrible things here */ }
    >};


    David is talking about const applied as a top level qualified,
    not a const member fucnction. I realize you might be trying
    to point of the benefits of const though; I doubt David
    disagrees about their benefits, just how they sh/c/ould come about.
    --
    Greg Comeau / Celebrating 20 years of Comeauity!
    Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
    World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
    Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
     
    Greg Comeau, Oct 13, 2005
    #10
  11. Greg Comeau Guest

    In article <y%p3f.11536$>,
    Risto Lankinen <> wrote:
    >"David White" <> wrote in message
    >news:...
    >> "Aleksey Loginov" <> wrote in message
    >> > it's makes difference when your const object behave not like no-const:
    >> >
    >> > struct test {
    >> > void operator () () { }
    >> > void operator () () const { /* terrible things here */ }
    >> > };

    >>
    >> I don't see how that relates to my post. If a function declares a
    >> pass-by-value parameter const, you are free to pass either a const or
    >> non-const value to it, since it's only the copy received by the function,
    >> not the original, that is declared const.

    >
    >void foo( test t )
    >{
    > t();
    >}
    >
    >void foo( test const t )
    >{
    > t(); /* those terrible things now happen */
    >}


    That's an invalid overload, IOWs: you can't do that.
    --
    Greg Comeau / Celebrating 20 years of Comeauity!
    Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
    World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
    Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
     
    Greg Comeau, Oct 13, 2005
    #11
  12. Greg Comeau Guest

    In article <>,
    mlimber <> wrote:
    >general principle of const-correctness is to make everything const
    >that can be const.


    The problem is that can and should are different :)
    --
    Greg Comeau / Celebrating 20 years of Comeauity!
    Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
    World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
    Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
     
    Greg Comeau, Oct 13, 2005
    #12
  13. Greg Comeau wrote:
    > In article <>,
    > Aleksey Loginov <> wrote:
    > >David White wrote:
    > >> wrote:
    > >> > I was going through a product's source code. They never use const
    > >> > function arguments with default value.
    > >> > for instance
    > >> >
    > >> > Never noticed following type of declaration
    > >> >
    > >> > foo ( const int param1, const bool = false) const;
    > >> >
    > >> > instead
    > >> > of this
    > >> > foo ( const int param1, bool = false) const; is used widely.
    > >> >
    > >> > I there any drawback of using default argument with const keyword?
    > >>
    > >> I don't think there's any point in making any pass-by-value parameter const,
    > >> default or not. The const only affects how the function can use the
    > >> parameter and makes no difference to the caller. Why should the public
    > >> interface declare that a function will not modify its own private copy of an
    > >> argument? IMO such consts clutter up the public interface with junk that is
    > >> really none of the caller's business.

    > >
    > >it's makes difference when your const object behave not like no-const:
    > >
    > >struct test {
    > > void operator () () { }
    > > void operator () () const { /* terrible things here */ }
    > >};

    >
    > David is talking about const applied as a top level qualified,
    > not a const member fucnction. I realize you might be trying
    > to point of the benefits of const though; I doubt David
    > disagrees about their benefits, just how they sh/c/ould come about.
    > --


    I agree with mlimber point "to make everything const that can be
    const".
    People, who working with your code after you, may say "Thanks" for that.
     
    Aleksey Loginov, Oct 14, 2005
    #13
  14. "Greg Comeau" <> wrote in message
    news:dilqcp$9s4$...
    > In article <y%p3f.11536$>,
    > Risto Lankinen <> wrote:
    > >"David White" <> wrote in message
    > >news:...
    > >> "Aleksey Loginov" <> wrote in message
    > >> > it's makes difference when your const object behave not like

    no-const:
    > >> >
    > >> > struct test {
    > >> > void operator () () { }
    > >> > void operator () () const { /* terrible things here */ }
    > >> > };
    > >>
    > >> I don't see how that relates to my post. If a function declares a
    > >> pass-by-value parameter const, you are free to pass either a const or
    > >> non-const value to it, since it's only the copy received by the

    function,
    > >> not the original, that is declared const.

    > >
    > >void foo( test t )
    > >{
    > > t();
    > >}
    > >
    > >void foo( test const t )
    > >{
    > > t(); /* those terrible things now happen */
    > >}

    >
    > That's an invalid overload, IOWs: you can't do that.


    Illustrates the point, though, does it not?

    If you indeed have hard time seeing the point, please re-read
    but replace "bar" for "foo" in one of the functions.

    - Risto -
     
    Risto Lankinen, Oct 14, 2005
    #14
  15. Greg Comeau Guest

    In article <mOI3f.11602$>,
    Risto Lankinen <> wrote:
    >"Greg Comeau" <> wrote in message
    >news:dilqcp$9s4$...
    >> In article <y%p3f.11536$>,
    >> Risto Lankinen <> wrote:
    >> >"David White" <> wrote in message
    >> >news:...
    >> >> "Aleksey Loginov" <> wrote in message
    >> >> > it's makes difference when your const object behave not like

    >no-const:
    >> >> >
    >> >> > struct test {
    >> >> > void operator () () { }
    >> >> > void operator () () const { /* terrible things here */ }
    >> >> > };
    >> >>
    >> >> I don't see how that relates to my post. If a function declares a
    >> >> pass-by-value parameter const, you are free to pass either a const or
    >> >> non-const value to it, since it's only the copy received by the

    >function,
    >> >> not the original, that is declared const.
    >> >
    >> >void foo( test t )
    >> >{
    >> > t();
    >> >}
    >> >
    >> >void foo( test const t )
    >> >{
    >> > t(); /* those terrible things now happen */
    >> >}

    >>
    >> That's an invalid overload, IOWs: you can't do that.

    >
    >Illustrates the point, though, does it not?
    >
    >If you indeed have hard time seeing the point, please re-read
    >but replace "bar" for "foo" in one of the functions.


    This thread had a few "the point"s. If you think we disagree
    somehow on one or more, just restate it instead of an editing session :)
    --
    Greg Comeau / Celebrating 20 years of Comeauity!
    Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
    World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
    Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
     
    Greg Comeau, Oct 14, 2005
    #15
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jim Strathmeyer

    stl list, const correctness

    Jim Strathmeyer, Mar 19, 2005, in forum: C++
    Replies:
    2
    Views:
    520
    Pete Becker
    Mar 20, 2005
  2. Matthias Kaeppler

    const-correctness and lambda expression

    Matthias Kaeppler, Apr 16, 2005, in forum: C++
    Replies:
    1
    Views:
    620
    Kanenas
    Apr 20, 2005
  3. Replies:
    10
    Views:
    582
    mlimber
    Oct 5, 2005
  4. Javier
    Replies:
    2
    Views:
    584
    James Kanze
    Sep 4, 2007
  5. fungus
    Replies:
    13
    Views:
    901
    fungus
    Oct 31, 2008
Loading...

Share This Page