Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Default value for a const reference parameter

Reply
Thread Tools

Default value for a const reference parameter

 
 
Senthilvel
Guest
Posts: n/a
 
      09-09-2004
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.



 
Reply With Quote
 
 
 
 
Tom Widmer
Guest
Posts: n/a
 
      09-09-2004
On Thu, 9 Sep 2004 16:01:50 +0530, "Senthilvel" <(E-Mail Removed)>
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
 
Reply With Quote
 
 
 
 
Stephan Br?nnimann
Guest
Posts: n/a
 
      09-09-2004
"Senthilvel" <(E-Mail Removed)> wrote in message news:<chpben$aht$(E-Mail Removed)>...
> 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
http://www.velocityreviews.com/forums/(E-Mail Removed)
Open source rating and billing engine for communication networks.
 
Reply With Quote
 
JKop
Guest
Posts: n/a
 
      09-09-2004
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
 
Reply With Quote
 
Howard
Guest
Posts: n/a
 
      09-09-2004

"JKop" <(E-Mail Removed)> wrote in message
news:WW%%c.26927$(E-Mail Removed)...
> 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




 
Reply With Quote
 
JKop
Guest
Posts: n/a
 
      09-09-2004
Howard posted:

>
> "JKop" <(E-Mail Removed)> wrote in message
> news:WW%%c.26927$(E-Mail Removed)...
>> 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
 
Reply With Quote
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      09-09-2004
JKop wrote:

> Howard posted:
>
>>
>> "JKop" <(E-Mail Removed)> wrote in message
>> news:WW%%c.26927$(E-Mail Removed)...
>>> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
is const necessary in eg int compar(const void *, const void *) lovecreatesbeauty@gmail.c0m C Programming 26 11-10-2008 09:47 PM
const correctness - should C++ prefer const member over non-const? fungus C++ 13 10-31-2008 05:33 AM
non-const reference and const reference George2 C++ 10 12-17-2007 02:19 PM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Casting int'** to 'const int * const * const' dosn't work, why? Jonas.Holmsten@gmail.com C Programming 11 07-01-2007 06:16 PM



Advertisments