Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Why doesn't the default argument allow const member?

Reply
Thread Tools

Why doesn't the default argument allow const member?

 
 
Divick
Guest
Posts: n/a
 
      09-21-2009
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
 
Reply With Quote
 
 
 
 
Thomas Lenz
Guest
Posts: n/a
 
      09-21-2009
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 ?

 
Reply With Quote
 
 
 
 
Francesco S. Carta
Guest
Posts: n/a
 
      09-21-2009
On 21 Set, 11:53, Divick <(E-Mail Removed)> 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
 
Reply With Quote
 
Ron
Guest
Posts: n/a
 
      09-21-2009
On Sep 21, 5:53*am, Divick <(E-Mail Removed)> 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.
 
Reply With Quote
 
Bo Persson
Guest
Posts: n/a
 
      09-21-2009
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




 
Reply With Quote
 
Francesco S. Carta
Guest
Posts: n/a
 
      09-21-2009
On 21 Set, 20:22, "Bo Persson" <(E-Mail Removed)> 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
 
Reply With Quote
 
Divick
Guest
Posts: n/a
 
      09-22-2009
On Sep 21, 11:22*pm, "Bo Persson" <(E-Mail Removed)> 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
 
Reply With Quote
 
Divick
Guest
Posts: n/a
 
      09-22-2009
On Sep 21, 5:14*pm, Ron <(E-Mail Removed)> wrote:
> On Sep 21, 5:53*am, Divick <(E-Mail Removed)> 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
 
Reply With Quote
 
Bart van Ingen Schenau
Guest
Posts: n/a
 
      09-22-2009
On Sep 22, 10:18*am, Divick <(E-Mail Removed)> wrote:
> On Sep 21, 5:14*pm, Ron <(E-Mail Removed)> wrote:
>
> > On Sep 21, 5:53*am, Divick <(E-Mail Removed)> 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
 
Reply With Quote
 
Fred Zwarts
Guest
Posts: n/a
 
      09-22-2009
>"Bart van Ingen Schenau" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
>On Sep 22, 10:18 am, Divick <(E-Mail Removed)> wrote:
>> On Sep 21, 5:14 pm, Ron <(E-Mail Removed)> wrote:
>>
>> > On Sep 21, 5:53 am, Divick <(E-Mail Removed)> 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?
 
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
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
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM



Advertisments