Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Why doesn't the default argument allow const member? (http://www.velocityreviews.com/forums/t698925-why-doesnt-the-default-argument-allow-const-member.html)

Divick 09-21-2009 09:53 AM

Why doesn't the default argument allow const member?
 
Hi,

I have the following code (see below). My compiler g++ 3.4.6 doesn't
compile it. I wonder why shouldn't this be allowed? I understand that
const int a, would be different for different objects, unlike static
const. And functions are common for all objects. But from the
compiler's perspective, shouldn't it be simple to handle as for every
invocation of a function with default argument, if the value is not
supplied then the compiler can simply initialize the function with the
value on which the function has been called?

class x
{
private :
const int a;
public :
X() : a(1)
{}

void foo(int arg = a)
{
cout << arg << endl;
}

};

Thanks in advance,
-DK

Thomas Lenz 09-21-2009 10:04 AM

Re: Why doesn't the default argument allow const member?
 
Divick wrote:

> Hi,
>
> I have the following code (see below). My compiler g++ 3.4.6 doesn't
> compile it. I wonder why shouldn't this be allowed? I understand that
> const int a, would be different for different objects, unlike static
> const. And functions are common for all objects. But from the
> compiler's perspective, shouldn't it be simple to handle as for every
> invocation of a function with default argument, if the value is not
> supplied then the compiler can simply initialize the function with the
> value on which the function has been called?
>
> class x
> {
> private :
> const int a;
> public :
> X() : a(1)
> {}
>
> void foo(int arg = a)
> {
> cout << arg << endl;
> }
>
> };
>
> Thanks in advance,
> -DK


Is it just a typo, or is your class name x but your Ctor (capital) X ?


Francesco S. Carta 09-21-2009 10:40 AM

Re: Why doesn't the default argument allow const member?
 
On 21 Set, 11:53, Divick <divick.kish...@gmail.com> wrote:
> Hi,
>
> I have the following code (see below). My compiler g++ 3.4.6 doesn't
> compile it. I wonder why shouldn't this be allowed? I understand that
> const int a, would be different for different objects, unlike static
> const. And functions are common for all objects. But from the
> compiler's perspective, shouldn't it be simple to handle as for every
> invocation of a function with default argument, if the value is not
> supplied then the compiler can simply initialize the function with the
> value on which the function has been called?
>
> class x
> {
> private :
> const int a;
> public :
> X() : a(1)
> {}
>
> void foo(int arg = a)
> {
> cout << arg << endl;
> }
>
> };
>
> Thanks in advance,
> -DK


If I got it right, members functions aren't much different from normal
functions, that is, you should think of your "foo(int)" as something
like "foo(int, A* this)", because the function itself is not aware of
the object it is called upon and needs the "hidden 'this' argument" to
be aware of it.

Unfortunately you cannot even write something like "void foo(int arg =
this->a)", because the 'this' pointer is visible only inside of the
function's body.

If you need a workaround, one way could be to decide a range of values
that lead to use the const value, such as:
-------
class A {
const int i;
public:
A(int v = 0) : i(v) {}
int foo(int v = -1) {
if(v < 0) {
return i;
} else {
return v;
}
}
};
-------

For what is worth, hope that helps.

Have good time,
Francesco
--
Francesco S. Carta, hobbyist
http://fscode.altervista.org

Ron 09-21-2009 12:14 PM

Re: Why doesn't the default argument allow const member?
 
On Sep 21, 5:53*am, Divick <divick.kish...@gmail.com> wrote:
> Hi,
>
> I have the following code (see below). My compiler g++ 3.4.6 doesn't
> compile it. I wonder why shouldn't this be allowed?


It's not allowed. The default arg substitution is done by the
caller.

Bo Persson 09-21-2009 06:22 PM

Re: Why doesn't the default argument allow const member?
 
Divick wrote:
> Hi,
>
> I have the following code (see below). My compiler g++ 3.4.6 doesn't
> compile it. I wonder why shouldn't this be allowed? I understand
> that const int a, would be different for different objects, unlike
> static const. And functions are common for all objects. But from the
> compiler's perspective, shouldn't it be simple to handle as for
> every invocation of a function with default argument, if the value
> is not supplied then the compiler can simply initialize the
> function with the value on which the function has been called?
>
> class x
> {
> private :
> const int a;
> public :
> X() : a(1)
> {}
>
> void foo(int arg = a)
> {
> cout << arg << endl;
> }
>
> };
>


It is not actually a bad idea, however it is expressly forbidden by
the standard. Default arguments cannot use non-static members, or the
keyword 'this'.

If you make a static it will work, but of course share its value
between all x objects.


As a workaround, you can add an overload that gets the correct value:

void foo()
{
foo(a);
}


Bo Persson





Francesco S. Carta 09-21-2009 06:30 PM

Re: Why doesn't the default argument allow const member?
 
On 21 Set, 20:22, "Bo Persson" <b...@gmb.dk> wrote:
> Divick wrote:
> > Hi,

>
> > I have the following code (see below). My compiler g++ 3.4.6 doesn't
> > compile it. I wonder why shouldn't this be allowed? I understand
> > that const int a, would be different for different objects, unlike
> > static const. And functions are common for all objects. But from the
> > compiler's perspective, shouldn't it be simple to handle as for
> > every invocation of a function with default argument, if the value
> > is not supplied then the compiler can simply initialize the
> > function with the value on which the function has been called?

>
> > class x
> > {
> > private :
> > * * * const int a;
> > public :
> > * * * X() : a(1)
> > * * * {}

>
> > * * * void foo(int arg = a)
> > * * * {
> > * * * * * * * cout << arg << endl;
> > * * * }

>
> > };

>
> It is not actually a bad idea, however it is expressly forbidden by
> the standard. Default arguments cannot use non-static members, or the
> keyword 'this'.
>
> If you make a static it will work, but of course share its value
> between all x objects.
>
> As a workaround, you can add an overload that gets the correct value:
>
> * * *void foo()
> * * *{
> * * * * * foo(a);
> * * *}


Which is way better than my proposition, thanks for pointing this out
Bo.

Have good time,
Francesco
--
Francesco S. Carta, hobbyist
http://fscode.altervista.org

Divick 09-22-2009 08:16 AM

Re: Why doesn't the default argument allow const member?
 
On Sep 21, 11:22*pm, "Bo Persson" <b...@gmb.dk> wrote:
> Divick wrote:
> > Hi,

>
> > I have the following code (see below). My compiler g++ 3.4.6 doesn't
> > compile it. I wonder why shouldn't this be allowed? I understand
> > that const int a, would be different for different objects, unlike
> > static const. And functions are common for all objects. But from the
> > compiler's perspective, shouldn't it be simple to handle as for
> > every invocation of a function with default argument, if the value
> > is not supplied then the compiler can simply initialize the
> > function with the value on which the function has been called?

>
> > class x
> > {
> > private :
> > * * * const int a;
> > public :
> > * * * X() : a(1)
> > * * * {}

>
> > * * * void foo(int arg = a)
> > * * * {
> > * * * * * * * cout << arg << endl;
> > * * * }

>
> > };

>
> It is not actually a bad idea, however it is expressly forbidden by
> the standard. Default arguments cannot use non-static members, or the
> keyword 'this'.
>
> If you make a static it will work, but of course share its value
> between all x objects.
>
> As a workaround, you can add an overload that gets the correct value:
>
> * * *void foo()
> * * *{
> * * * * * foo(a);
> * * *}
>
> Bo Persson


I think I could guess that it is not allowed by the standard, but my
question was originally about why it is not allowed. Unless there is a
good reason for disallowing it in the standard, the standard could
have allowed non static default argument as well.

Thanks for the replies,
DK

Divick 09-22-2009 08:18 AM

Re: Why doesn't the default argument allow const member?
 
On Sep 21, 5:14*pm, Ron <ron.nata...@gmail.com> wrote:
> On Sep 21, 5:53*am, Divick <divick.kish...@gmail.com> wrote:
>
> > Hi,

>
> > I have the following code (see below). My compiler g++ 3.4.6 doesn't
> > compile it. I wonder why shouldn't this be allowed?

>
> It's not allowed. * The default arg substitution is done by the
> caller.


The question is why is it not allowed? As long as arguments are known
at compile time, the compiler could easily substitute the default
argument be it static or non static.

Thanks for the reply,
DK

Bart van Ingen Schenau 09-22-2009 11:50 AM

Re: Why doesn't the default argument allow const member?
 
On Sep 22, 10:18*am, Divick <divick.kish...@gmail.com> wrote:
> On Sep 21, 5:14*pm, Ron <ron.nata...@gmail.com> wrote:
>
> > On Sep 21, 5:53*am, Divick <divick.kish...@gmail.com> wrote:

>
> > > Hi,

>
> > > I have the following code (see below). My compiler g++ 3.4.6 doesn't
> > > compile it. I wonder why shouldn't this be allowed?

>
> > It's not allowed. * The default arg substitution is done by the
> > caller.

>
> The question is why is it not allowed? As long as arguments are known
> at compile time, the compiler could easily substitute the default
> argument be it static or non static.


Really?
How does the compiler know what to substitute in these lines:

int main()
{
x foo, bar;
foo.foo();
bar.foo();
}

Remember that the default argument 'a' must be accessible from the
context of main().

>
> Thanks for the reply,
> DK


Bart v Ingen Schenau

Fred Zwarts 09-22-2009 12:28 PM

Re: Why doesn't the default argument allow const member?
 
>"Bart van Ingen Schenau" <bart@ingen.ddns.info> wrote in message news:add373d2-9798-40b0-92ea-fcd456d76f7a@m11g2000vbl.googlegroups.com...
>On Sep 22, 10:18 am, Divick <divick.kish...@gmail.com> wrote:
>> On Sep 21, 5:14 pm, Ron <ron.nata...@gmail.com> wrote:
>>
>> > On Sep 21, 5:53 am, Divick <divick.kish...@gmail.com> wrote:

>>
>> > > Hi,

>>
>> > > I have the following code (see below). My compiler g++ 3.4.6 doesn't
>> > > compile it. I wonder why shouldn't this be allowed?

>>
>> > It's not allowed. The default arg substitution is done by the
>> > caller.

>>
>> The question is why is it not allowed? As long as arguments are known
>> at compile time, the compiler could easily substitute the default
>> argument be it static or non static.

>
>Really?
>How does the compiler know what to substitute in these lines:
>
>int main()
>{
>x foo, bar;
>foo.foo();
>bar.foo();
>}
>
>Remember that the default argument 'a' must be accessible from the
>context of main().


Could you elaborate?
If the non-static member function "foo ()" of "x" is accessible by the compiler from the context of main (),
it means that the declaration of "x" is within the context of main.
The declaration of "x" includes the default parameter value "a" for the function "foo ()".
Why then should the non-static member "a" of "x" be inaccessible by the compiler from the context of main?


All times are GMT. The time now is 08:08 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.