Default value for a const reference parameter

S

Senthilvel

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.
 
T

Tom Widmer

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
 
S

Stephan Br?nnimann

Senthilvel said:
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 ?



Thanks and Best Regards,
Senthil.

Stephan Brönnimann
(e-mail address removed)
Open source rating and billing engine for communication networks.
 
J

JKop

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
 
H

Howard

JKop said:
Senthilvel posted:


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
 
J

JKop

Howard posted:
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
 
K

Kai-Uwe Bux

JKop said:
Howard posted:


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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top