473,903 Members | 3,835 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Default value for a const reference parameter

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.

Jul 22 '05 #1
6 4877
On Thu, 9 Sep 2004 16:01:50 +0530, "Senthilvel " <se*****@nospam .com>
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
Jul 22 '05 #2
"Senthilvel " <se*****@nospam .com> wrote in message news:<ch******* ***@news.mch.sb s.de>...
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
br****@osb-systems.com
Open source rating and billing engine for communication networks.
Jul 22 '05 #3
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
Jul 22 '05 #4

"JKop" <NU**@NULL.NULL > wrote in message
news:WW******** **********@news .indigo.ie...
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


Jul 22 '05 #5
Howard posted:

"JKop" <NU**@NULL.NULL > wrote in message
news:WW******** **********@news .indigo.ie...
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
Jul 22 '05 #6
JKop wrote:
Howard posted:

"JKop" <NU**@NULL.NULL > wrote in message
news:WW******** **********@news .indigo.ie...
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

Jul 22 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
2593
by: Steven T. Hatton | last post by:
As far as I know, there is no way to provide a default value for the argument to the constructor A::A(char (&array)) in this example. Correct? struct A{ A(char (&array) ){ std::copy(&array,&array,&_array); } char _array; };
4
3556
by: Rein Anders Apeland | last post by:
Consider the following working code: #include <memory> #include <iostream> void print_ptr( const std::auto_ptr< int > & thePtr = std::auto_ptr< int >() ) {
4
12330
by: Carlos Gomez | last post by:
In VB6 the default for passing variables was ByRef. It was faster and used less memory. Why did MS changed that? Are there any advantages using ByVal over ByRef? (other than ByVal impeding you from changing the original variable passed)
12
2121
by: huangshan | last post by:
hi all In what condition i need( or mast) a (templates)function return value by reference? can you give me a example thanks
12
2339
by: aaragon | last post by:
Hello all. I have a simple question that seems trivial but I can't make it to work. I have a class that takes as a template argument, another class. The idea is as follows: #include <iostream> using namespace std; template <class ClassB> class ClassA
16
2401
by: jryden | last post by:
I can do this: struct myInt { int i; }; void func1(myInt &i = myInt()); but not this:
3
8821
by: Gary Wessle | last post by:
I have a method with the last arguments having defaults like this void filing(long, double, const myType& r_mo=NULL, const herType& r_tr=NULL); is that acceptable, if not how is it done? when I do the above, I get errors like file.h:19: error: default argument for parameter of type ‘const myType&’ has type ‘int’
14
20433
by: Abhi | last post by:
I wrote a function foo(int arr) and its prototype is declared as foo(int arr); I modify the values of the array in the function and the values are getting modified in the main array which is passed also. I understand that this way of passing the array is by value and if the prototype is declared as foo(int *), it is by reference in which case the value if modified in the function will get reflected in the main function as well. I dont...
9
3832
by: George2 | last post by:
Hello everyone, I am wondering the default implementation of assignment operator (e.g. when we do not implement assignment operator in user defined class, what will be returned? temporary object? reference or const reference? deep copy or shallow copy is used in default assignment operator?)? I have the C++ Programming Book at hand, but can not find it from Index page.
0
9853
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10882
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10504
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9692
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8057
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5899
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6100
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4734
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3327
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.