Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old November 10th, 2006, 03:05 PM
jryden
Guest
 
Posts: n/a
Default Default reference variables

I can do this:

struct myInt
{
int i;
};
void func1(myInt &i = myInt());

but not this:

void func2(int &i = int());

Generates error C2440: 'default argument' : cannot convert from 'int'
to 'int &' (Visual C++ compiler)

Can someone explain why? I would like a default reference parameter
using a simple type.

Thanks,
James

  #2  
Old November 10th, 2006, 03:15 PM
Victor Bazarov
Guest
 
Posts: n/a
Default Re: Default reference variables

jryden wrote:
Quote:
I can do this:
>
struct myInt
{
int i;
};
void func1(myInt &i = myInt());
No, you can't. A reference to non-const object (sometimes referred to
as "a const reference") cannot be bound to a temporary.
Quote:
>
but not this:
>
void func2(int &i = int());
>
Generates error C2440: 'default argument' : cannot convert from 'int'
to 'int &' (Visual C++ compiler)
>
Can someone explain why? I would like a default reference parameter
using a simple type.
Do you have to have it non-const? What for?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


  #3  
Old November 10th, 2006, 03:25 PM
Greg
Guest
 
Posts: n/a
Default Re: Default reference variables


jryden wrote:
Quote:
I can do this:
>
struct myInt
{
int i;
};
void func1(myInt &i = myInt());
>
but not this:
>
void func2(int &i = int());
The parameter reference needs to be declared const in order to pass a
temporary:

void func2(const int& = int())

The assumption is that the program is calling func2() in order to
obtain the value it returns through the non-const parameter. So if the
program just throws that value away, why is it calling func2() in the
first place? Or, why isn't func2()'s parameter const?

Greg

  #4  
Old November 10th, 2006, 03:25 PM
Puppet_Sock
Guest
 
Posts: n/a
Default Re: Default reference variables

jryden wrote:
[snip]
Quote:
Can someone explain why? I would like a default reference parameter
using a simple type.
Urm. Why do you want to default an argument that is a reference?
I'd expect that if you called a function without an argument, and it
wanted a reference, that this would be an error. I'm presuming that
the reason to pass a reference is so that the function can change
the value of the calling code's variable. If you only want the value
to be passed down, and not access to it, at least for a simple type,
why pass a reference?

Also, what's wrong with overloading with a version of the func that
has no arguments?
Socks

  #5  
Old November 10th, 2006, 03:25 PM
jryden
Guest
 
Posts: n/a
Default Re: Default reference variables


Victor Bazarov wrote:
Quote:
jryden wrote:
Quote:
I can do this:

struct myInt
{
int i;
};
void func1(myInt &i = myInt());
>
No, you can't. A reference to non-const object (sometimes referred to
as "a const reference") cannot be bound to a temporary.
Perhaps I should rephrase this to "my compiler allows me to do this".
In any case it does what I intend it to do...(Microsoft C++?)
Quote:
>
Quote:

but not this:

void func2(int &i = int());

Generates error C2440: 'default argument' : cannot convert from 'int'
to 'int &' (Visual C++ compiler)

Can someone explain why? I would like a default reference parameter
using a simple type.
>
Do you have to have it non-const? What for?
My intention is to have a return variable, therefore, non-const (an
[out] parameter) but I'd like the parameter to have a default value.
This is so that the hundreds of other calls to this function in my code
base don't need to be modified to pass this value as a parameter if
they don't care about what it returns.

This is typically done by doing the following:
void func3(int *i = NULL)
{
if (i = NULL) *i = x;
}

This is a constuction is don't like especially if there are many places
in the function where we would like to set the value of i.

Is there a better way to accomplish the this?
Quote:
>
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
  #6  
Old November 10th, 2006, 03:35 PM
Alf P. Steinbach
Guest
 
Posts: n/a
Default Re: Default reference variables

* jryden:
Quote:
I can do this:
>
struct myInt
{
int i;
};
void func1(myInt &i = myInt());
As Victor already wrote, no you can't.

Adding to that, some compilers allow you to do that as a non-standard
extension.

If your compiler is one such, then it's a good idea to turn off that
language extension, if possible.

Quote:
but not this:
>
void func2(int &i = int());
>
Generates error C2440: 'default argument' : cannot convert from 'int'
to 'int &' (Visual C++ compiler)
Right.

Quote:
Can someone explain why?
Stroustrup's original explanation for why an alias for (reference to) a
non-const can't be bound to an rvalue, is that that could easily lead to
surprising results like apparently modifying the value of 42 -- while
in reality of course just modifying the temporary. I seem to recall
that Pete Becker once posted a much more forceful argument in this
group, involving function arguments and overloading. Something like
calling with an int argument for a formal unsigned& -- I can't recall
-- but now that I've mentioned it, you would not want this

void doTheThing( unsigned& x ) { x = 666; }

int main()
{
int nastyValue;
doTheThing( nastyValue );
std::cout << nastyValue << std::endl;
}

to be accepted, much less produce garbage output.

Quote:
I would like a default [presumably non-const] reference parameter using
a simple type.
You can do

void func2( int& i ) { ... }
void func2() { int i = 0; func2( i ); }

or you can do

int defaultI = 0;

void func2( int& i = ::defaultI ) { ... }

or whatever.

--
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?
  #7  
Old November 10th, 2006, 03:35 PM
Howard
Guest
 
Posts: n/a
Default Re: Default reference variables


"jryden" <jryden@gmail.comwrote in message
news:1163173715.273966.305200@b28g2000cwb.googlegr oups.com...
Quote:
>
Quote:
>
My intention is to have a return variable, therefore, non-const (an
[out] parameter) but I'd like the parameter to have a default value.
This is so that the hundreds of other calls to this function in my code
base don't need to be modified to pass this value as a parameter if
they don't care about what it returns.
>
This is typically done by doing the following:
void func3(int *i = NULL)
{
if (i = NULL) *i = x;
that should be
if (i == NULL)

Some people prefer
if (NULL == i)

That way, if you use = accidently, the compiler will complain, since you
can't assign to NULL.
Quote:
}
>
This is a constuction is don't like especially if there are many places
in the function where we would like to set the value of i.
>
Is there a better way to accomplish the this?
>
I've done this some times:

void f( int* pInt )
{
int temp;
if (!pInt)
pInt = &temp;
...
*pInt = whatever;
...
*pInt = whateverelse;
...
}

That lets me do the NULL check just once at the top of the function, and
keep the code below clean.

-Howard



  #8  
Old November 10th, 2006, 03:45 PM
jryden
Guest
 
Posts: n/a
Default Re: Default reference variables



On Nov 10, 10:58 am, "Howard" <alic...@hotmail.comwrote:
Quote:
"jryden" <jry...@gmail.comwrote in messagenews:1163173715.273966.305200@b28g2000cwb.g ooglegroups.com...
>
>
>
Quote:
My intention is to have a return variable, therefore, non-const (an
[out] parameter) but I'd like the parameter to have a default value.
This is so that the hundreds of other calls to this function in my code
base don't need to be modified to pass this value as a parameter if
they don't care about what it returns.
>
Quote:
This is typically done by doing the following:
void func3(int *i = NULL)
{
if (i = NULL) *i = x;that should be
if (i == NULL)
>
Some people prefer
if (NULL == i)
>
That way, if you use = accidently, the compiler will complain, since you
can't assign to NULL.
>
Quote:
}
>
Quote:
This is a constuction is don't like especially if there are many places
in the function where we would like to set the value of i.
>
Quote:
Is there a better way to accomplish the this?I've done this some times:
>
void f( int* pInt )
{
int temp;
if (!pInt)
pInt = &temp;
...
*pInt = whatever;
...
*pInt = whateverelse;
...
>
}That lets me do the NULL check just once at the top of the function, and
keep the code below clean.
>
-Howard
Okay, I think I have a couple of more standard solutions now. Thanks
for the info.

  #9  
Old November 10th, 2006, 06:35 PM
David Harmon
Guest
 
Posts: n/a
Default Re: Default reference variables

On Fri, 10 Nov 2006 10:37:36 -0500 in comp.lang.c++, "Victor Bazarov"
<v.Abazarov@comAcast.netwrote,
Quote:
Quote:
>void func1(myInt &i = myInt());
>
>No, you can't. A reference to non-const object (sometimes referred to
>as "a const reference") cannot be bound to a temporary.
It's OK if myInt() returns a reference.

  #10  
Old November 10th, 2006, 06:55 PM
Andrey Tarasevich
Guest
 
Posts: n/a
Default Re: Default reference variables

David Harmon wrote:
Quote:
Quote:
Quote:
>>void func1(myInt &i = myInt());
>>
>>No, you can't. A reference to non-const object (sometimes referred to
>>as "a const reference") cannot be bound to a temporary.
>
It's OK if myInt() returns a reference.
:) Great idea! I wonder, however, if it is possible to come up with a correct
set of declarations in order to turn the OP's declaration into a valid one. This
is close

struct myInt {};
myInt& myInt();

void foo(struct myInt& i = myInt())
{
}

but if I remove the 'struct' from the parameter declaration Comeau will complain
with

"ComeauTest.c", line 5: error: incomplete type is not allowed
void foo(myInt& i = myInt())
^
"ComeauTest.c", line 5: error: identifier "i" is undefined
void foo(myInt& i = myInt())
^

--
Best regards,
Andrey Tarasevich
  #11  
Old November 10th, 2006, 08:15 PM
Bo Persson
Guest
 
Posts: n/a
Default Re: Default reference variables

jryden wrote:
Quote:
Victor Bazarov wrote:
Quote:
>jryden wrote:
Quote:
>>I can do this:
>>>
>>struct myInt
>>{
>> int i;
>>};
>>void func1(myInt &i = myInt());
>>
>No, you can't. A reference to non-const object (sometimes
>referred to as "a const reference") cannot be bound to a temporary.
>
Perhaps I should rephrase this to "my compiler allows me to do
this". In any case it does what I intend it to do...(Microsoft C++?)
>
That's because you use a compiler specific language extension. If you try it
with a /Za compiler option, it will not compile.


Bo Persson


  #12  
Old November 10th, 2006, 10:35 PM
David Harmon
Guest
 
Posts: n/a
Default Re: Default reference variables

On Fri, 10 Nov 2006 11:09:20 -0800 in comp.lang.c++, Andrey Tarasevich
<andreytarasevich@hotmail.comwrote,
Quote:
>David Harmon wrote:
Quote:
Quote:
>>>void func1(myInt &i = myInt());
>>>
>>>No, you can't. A reference to non-const object (sometimes referred to
>>>as "a const reference") cannot be bound to a temporary.
>>
>It's OK if myInt() returns a reference.
>
>:) Great idea! I wonder, however, if it is possible to come up with a correct
>set of declarations in order to turn the OP's declaration into a valid one.
Ouch; I failed to notice that myInt is also the type.
I was thinking more in the line of a singleton of type int.
myInt ... rather bad type name I think.

  #13  
Old November 10th, 2006, 10:55 PM
jryden
Guest
 
Posts: n/a
Default Re: Default reference variables



On Nov 10, 3:31 pm, "Bo Persson" <b...@gmb.dkwrote:
Quote:
jryden wrote:
Quote:
Victor Bazarov wrote:
Quote:
jryden wrote:
>I can do this:
>
Quote:
Quote:
>struct myInt
>{
> int i;
>};
>void func1(myInt &i = myInt());
>
Quote:
Quote:
No, you can't. A reference to non-const object (sometimes
referred to as "a const reference") cannot be bound to a temporary.
>
Quote:
Perhaps I should rephrase this to "my compiler allows me to do
this". In any case it does what I intend it to do...(Microsoft C++?)That's because you use a compiler specific language extension. If you try it
with a /Za compiler option, it will not compile.
....Neither will many other windows specific core files (specifically
ATL). Interestingly microsoft documentation explains that /Ze,
although deprecated, is also the default setting. I'll have to leave
it on for now but if and when we decide to port our code to another
platform we might have some interesting problems...but this is off
topic now.
Quote:
>
Bo Persson- Hide quoted text -- Show quoted text -
  #14  
Old November 10th, 2006, 10:55 PM
jryden
Guest
 
Posts: n/a
Default Re: Default reference variables



On Nov 10, 5:53 pm, David Harmon <sou...@netcom.comwrote:
Quote:
On Fri, 10 Nov 2006 11:09:20 -0800 in comp.lang.c++, Andrey Tarasevich
<andreytarasev...@hotmail.comwrote,
>
Quote:
David Harmon wrote:
Quote:
>>void func1(myInt &i = myInt());
>
Quote:
Quote:
>>No, you can't. A reference to non-const object (sometimes referred to
>>as "a const reference") cannot be bound to a temporary.
>
Quote:
Quote:
It's OK if myInt() returns a reference.
>
Quote:
:) Great idea! I wonder, however, if it is possible to come up with a correct
set of declarations in order to turn the OP's declaration into a valid one.Ouch;
Quote:
I failed to notice that myInt is also the type.
I was thinking more in the line of a singleton of type int.
myInt ... rather bad type name I think.
I have lost the train of this thread, I think. myInt was just a quick
example struct from the original post (perhaps it should be MyInt)
anyway, I have been conviced that function overloading is better way to
accomplish my goal and I have changed my code accordingly.

  #15  
Old November 10th, 2006, 11:25 PM
Andrey Tarasevich
Guest
 
Posts: n/a
Default Re: Default reference variables

David Harmon wrote:
Quote:
Quote:
Quote:
>>>>void func1(myInt &i = myInt());
>>>>
>>>>No, you can't. A reference to non-const object (sometimes referred to
>>>>as "a const reference") cannot be bound to a temporary.
>>>
>>It's OK if myInt() returns a reference.
>>
>>:) Great idea! I wonder, however, if it is possible to come up with a correct
>>set of declarations in order to turn the OP's declaration into a valid one.
>
Ouch; I failed to notice that myInt is also the type.
I was thinking more in the line of a singleton of type int.
myInt ... rather bad type name I think.
Yet it can be done :) I just had an idea

struct myInt {};
myInt& bar();
#define myInt() bar()

void foo(myInt& i = myInt())
{
}

--
Best regards,
Andrey Tarasevich
  #16  
Old November 12th, 2006, 02:05 AM
programmer
Guest
 
Posts: n/a
Default Re: Default reference variables


"jryden дµÀ£º
"
Quote:
I can do this:
>
struct myInt
{
int i;
};
void func1(myInt &i = myInt());
>
but not this:
>
void func2(int &i = int());
>
I think a reference is just a symbol.This symbol is just occur in our
code.When call such function that take references as arguments, they
will be replaced with the actual addresses of parameters which you
reference to.When you dasm your program you will find out how it
work.You can just using this
voi func2(int &i = *(new int))
Thus,it enable i to reference to a address that store a int.

  #17  
Old November 12th, 2006, 02:35 AM
programmer
Guest
 
Posts: n/a
Default Re: Default reference variables


"programmer дµÀ£º
"
Quote:
"jryden дµÀ£º
"
Quote:
I can do this:

struct myInt
{
int i;
};
void func1(myInt &i = myInt());

but not this:

void func2(int &i = int());
I think a reference is just a symbol.This symbol is just occur in our
code.When call such function that take references as arguments, they
will be replaced with the actual addresses of parameters which you
reference to.When you dasm your program you will find out how it
work.You can just using this
voi func2(int &i = *(new int))
Thus,it enable i to reference to a address that store a int.
I'm sorry.I have made a big problem.Never try to use this.It has a
problem with how you release your memory allocated by new.It just tell
you how reference works.

 

Bookmarks

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 Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles