Default value for a const reference parameter

Discussion in 'C++' started by Senthilvel, Sep 9, 2004.

  1. Senthilvel

    Senthilvel Guest

    Hi folks,
    My friend tells that the following function declaration is illegal.

    void Foo(const string& strData = "Default");

    My friend argues that it is not legal to provide a default value for a
    parameter passed by reference or const reference.
    But i do not think so.
    I looked up in the TCPPPL but nothing of that sort is explicitly
    mentioned(or have i missed it ??? ).
    Can someone solve the dispute ?

    Thanks and Best Regards,
    Senthil.
    Senthilvel, Sep 9, 2004
    #1
    1. Advertising

  2. Senthilvel

    Tom Widmer Guest

    On Thu, 9 Sep 2004 16:01:50 +0530, "Senthilvel" <>
    wrote:

    >Hi folks,
    >My friend tells that the following function declaration is illegal.
    >
    >void Foo(const string& strData = "Default");
    >
    >My friend argues that it is not legal to provide a default value for a
    >parameter passed by reference or const reference.
    >But i do not think so.
    >I looked up in the TCPPPL but nothing of that sort is explicitly
    >mentioned(or have i missed it ??? ).
    >Can someone solve the dispute ?


    The code is fine. You can't do it for a non-const reference, since you
    can't bind a non-const reference to a temporary. Basically, the
    default parameter can be anything you could pass manually.

    Tom
    Tom Widmer, Sep 9, 2004
    #2
    1. Advertising

  3. "Senthilvel" <> wrote in message news:<chpben$aht$>...
    > Hi folks,
    > My friend tells that the following function declaration is illegal.

    That's correct: what is `string'?
    >
    > void Foo(const string& strData = "Default");
    >
    > My friend argues that it is not legal to provide a default value for a
    > parameter passed by reference or const reference.

    He's wrong
    > But i do not think so.
    > I looked up in the TCPPPL but nothing of that sort is explicitly
    > mentioned(or have i missed it ??? ).
    > Can someone solve the dispute ?



    #include <string>
    void Foo(const std::string& strData = "Default");

    >
    > Thanks and Best Regards,
    > Senthil.


    Stephan Brönnimann

    Open source rating and billing engine for communication networks.
    Stephan Br?nnimann, Sep 9, 2004
    #3
  4. Senthilvel

    JKop Guest

    Senthilvel posted:

    > Hi folks,
    > My friend tells that the following function declaration

    is illegal.
    >
    > void Foo(const string& strData = "Default");
    >
    > My friend argues that it is not legal to provide a

    default value for a
    > parameter passed by reference or const reference.
    > But i do not think so.
    > I looked up in the TCPPPL but nothing of that sort is

    explicitly
    > mentioned(or have i missed it ??? ).
    > Can someone solve the dispute ?
    >
    > Thanks and Best Regards,
    > Senthil.
    >
    >
    >


    Try
    compile
    the
    bleeding
    thing

    -Jkop
    JKop, Sep 9, 2004
    #4
  5. Senthilvel

    Howard Guest

    "JKop" <> wrote in message
    news:WW%%c.26927$...
    > Senthilvel posted:
    >
    > > Hi folks,
    > > My friend tells that the following function declaration

    > is illegal.
    > >
    > > void Foo(const string& strData = "Default");
    > >
    > > My friend argues that it is not legal to provide a

    > default value for a
    > > parameter passed by reference or const reference.
    > > But i do not think so.
    > > I looked up in the TCPPPL but nothing of that sort is

    > explicitly
    > > mentioned(or have i missed it ??? ).
    > > Can someone solve the dispute ?
    > >
    > > Thanks and Best Regards,
    > > Senthil.
    > >
    > >
    > >

    >
    > Try
    > compile
    > the
    > bleeding
    > thing
    >
    > -Jkop


    Sometimes, that tells you very little. Especially if you're using a piece
    of junk like VC++6.0. The fact that compiler compiles it (which it does - I
    tried it) is no indication of its validity as standard-compliant code. :)

    -Howard
    Howard, Sep 9, 2004
    #5
  6. Senthilvel

    JKop Guest

    Howard posted:

    >
    > "JKop" <> wrote in message
    > news:WW%%c.26927$...
    >> Senthilvel posted:
    >>
    >> > Hi folks,
    >> > My friend tells that the following function

    declaration is illegal.
    >> >
    >> > void Foo(const string& strData = "Default");
    >> >
    >> > My friend argues that it is not legal to provide a

    default value for
    >> > a parameter passed by reference or const reference.
    >> > But i do not think so.
    >> > I looked up in the TCPPPL but nothing of that sort is

    explicitly
    >> > mentioned(or have i missed it ??? ).
    >> > Can someone solve the dispute ?
    >> >
    >> > Thanks and Best Regards,
    >> > Senthil.
    >> >
    >> >
    >> >

    >>
    >> Try
    >> compile
    >> the
    >> bleeding
    >> thing
    >>
    >> -Jkop

    >
    > Sometimes, that tells you very little. Especially if

    you're using a
    > piece of junk like VC++6.0. The fact that compiler

    compiles it (which
    > it does - I tried it) is no indication of its validity as
    > standard-compliant code. :)
    >
    > -Howard


    I never commented on whether it was legal code or not.

    -JKop
    JKop, Sep 9, 2004
    #6
  7. Senthilvel

    Kai-Uwe Bux Guest

    JKop wrote:

    > Howard posted:
    >
    >>
    >> "JKop" <> wrote in message
    >> news:WW%%c.26927$...
    >>> Senthilvel posted:
    >>>
    >>> > Hi folks,
    >>> > My friend tells that the following function

    > declaration is illegal.
    >>> >
    >>> > void Foo(const string& strData = "Default");
    >>> >
    >>> > My friend argues that it is not legal to provide a

    > default value for
    >>> > a parameter passed by reference or const reference.
    >>> > But i do not think so.
    >>> > I looked up in the TCPPPL but nothing of that sort is

    > explicitly
    >>> > mentioned(or have i missed it ??? ).
    >>> > Can someone solve the dispute ?
    >>> >
    >>> > Thanks and Best Regards,
    >>> > Senthil.
    >>> >
    >>> >
    >>> >
    >>>
    >>> Try
    >>> compile
    >>> the
    >>> bleeding
    >>> thing
    >>>
    >>> -Jkop

    >>
    >> Sometimes, that tells you very little. Especially if

    > you're using a
    >> piece of junk like VC++6.0. The fact that compiler

    > compiles it (which
    >> it does - I tried it) is no indication of its validity as
    >> standard-compliant code. :)
    >>
    >> -Howard

    >
    > I never commented on whether it was legal code or not.


    True, but the OP specifically reports about a dispute as to wether the code
    is legal and asks how this dispute is to be resolved.



    Best

    Kai-Uwe Bux
    Kai-Uwe Bux, Sep 9, 2004
    #7
    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. Replies:
    11
    Views:
    1,086
  2. Javier
    Replies:
    2
    Views:
    542
    James Kanze
    Sep 4, 2007
  3. George2
    Replies:
    10
    Views:
    577
    Pete Becker
    Dec 17, 2007
  4. 0m
    Replies:
    26
    Views:
    1,089
    Tim Rentsch
    Nov 10, 2008
  5. fungus
    Replies:
    13
    Views:
    871
    fungus
    Oct 31, 2008
Loading...

Share This Page