Topic: Access rights for default arguments?


Author: Christopher Eltschka <celtschk@physik.tu-muenchen.de>
Date: 1998/10/26
Raw View
Nate Lewis wrote:
>
> Christopher Eltschka wrote in message
> <362DF293.6E9E7E25@physik.tu-muenchen.de>...
> >#include <iostream.h>
> >
> >class Test
> >{
> >public:
> >  Test(int i_d): i_default(i_d) {}
> >  void f(int i = i_default) { cout << i << endl; }
> >private:
> >  int i_default;
> >};
> >
> >Is the default argument in Test::f legal?
>
> No.
>
> "...a nonstatic member shall not be used in a default argument
> expression, even if it is not evaluated, unless it appears as the
> id-expression of a class member access expression or unless it is used
> to form a pointer to member." [8.3.6/9]  An example very like yours is
> also given.  Static members are all right; 'this' is also not allowed.
> It seems that the default argument of a function cannot depend on the
> instance it's being called on.

That's an interesting point I didn't know of. However, I don't
understand the reason of it.

Since

- default arguments are always resolved according to the static type,
- to call a non-static member function, the this pointer must be known
  to the caller anyway (for the implicit this parameter) and
- the definition of the class must be visible for the caller anyway
  (which includes visibility of the member),

I don't see any problem to generate code for this case.
However, I'm sure the commitee wouldn't have forbidden it without a
good reason. So what is it?
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: alanstokes@my-dejanews.com
Date: 1998/10/27
Raw View
In article <363441C6.EB22D16F@physik.tu-muenchen.de>,
  Christopher Eltschka <celtschk@physik.tu-muenchen.de> wrote:
> Nate Lewis wrote:
> > "...a nonstatic member shall not be used in a default argument
> > expression, even if it is not evaluated, unless it appears as the
> > id-expression of a class member access expression or unless it is used
> > to form a pointer to member." [8.3.6/9]

> I don't see any problem to generate code for this case.
> However, I'm sure the commitee wouldn't have forbidden it without a
> good reason. So what is it?

The order of evaluation of parameters is deliberately not defined, to allow
the compiler to do useful optimisations. So since the "this" pointer is
basically just another parameter, it might not be evaluated until after it
was needed for calculating the default argument - which would be bad.

It doesn't seem a good idea to add a special case to the order of evaluation
rules for this one case (especially since overloading can be used as an
alternative).

--
- Alan

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Christopher Eltschka <celtschk@physik.tu-muenchen.de>
Date: 1998/10/21
Raw View
Is the following code legal?

#include <iostream.h>

class Test
{
public:
  Test(int i_d): i_default(i_d) {}
  void f(int i = i_default) { cout << i << endl; }
private:
  int i_default;
};

int main()
{
  Test t(1);
  t.f(2);
  t.f();
}

My question is:

Is the default argument in Test::f legal?

(Yes, I know I can rewrite this by overloading f...)
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: sbnaran@fermi.ceg.uiuc.edu (Siemel Naran)
Date: 1998/10/21
Raw View
On 21 Oct 98 16:06:43 GMT, Christopher Eltschka

>Is the following code legal?

No, it is not legal as the subsitution of missing arguments by the
default arguments is done at compile time.

>class Test
>{
>public:
>  Test(int i_d): i_default(i_d) {}
>  void f(int i = i_default) { cout << i << endl; }
>private:
>  int i_default;
>};


However, you can use static variables

class Test
{
public:
  Test() {}
  static int i_default;
  void f(int i = i_default) { cout << i << endl; }
};


--
----------------------------------
Siemel B. Naran (sbnaran@uiuc.edu)
----------------------------------


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Nate Lewis" <nlewis@mindspring.com>
Date: 1998/10/21
Raw View
Christopher Eltschka wrote in message
<362DF293.6E9E7E25@physik.tu-muenchen.de>...
>#include <iostream.h>
>
>class Test
>{
>public:
>  Test(int i_d): i_default(i_d) {}
>  void f(int i = i_default) { cout << i << endl; }
>private:
>  int i_default;
>};
>
>Is the default argument in Test::f legal?


No.

"...a nonstatic member shall not be used in a default argument
expression, even if it is not evaluated, unless it appears as the
id-expression of a class member access expression or unless it is used
to form a pointer to member." [8.3.6/9]  An example very like yours is
also given.  Static members are all right; 'this' is also not allowed.
It seems that the default argument of a function cannot depend on the
instance it's being called on.

--
Nate Lewis, MCSD
nlewis@mindspring.com



[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: kanze@gabi-soft.fr (J. Kanze)
Date: 1998/10/22
Raw View
"Nate Lewis" <nlewis@mindspring.com> writes:

|>  Christopher Eltschka wrote in message
|>  <362DF293.6E9E7E25@physik.tu-muenchen.de>...
|>  >#include <iostream.h>

|>  >class Test
|>  >{
|>  >public:
|>  >  Test(int i_d): i_default(i_d) {}
|>  >  void f(int i = i_default) { cout << i << endl; }
|>  >private:
|>  >  int i_default;
|>  >};
|>  >
|>  >Is the default argument in Test::f legal?


|>  No.

|>  "...a nonstatic member shall not be used in a default argument
|>  expression, even if it is not evaluated, unless it appears as the
|>  id-expression of a class member access expression or unless it is used
|>  to form a pointer to member." [8.3.6/9]  An example very like yours is
|>  also given.  Static members are all right; 'this' is also not allowed.
|>  It seems that the default argument of a function cannot depend on the
|>  instance it's being called on.

Good point, but I think his real question was about the fact that the
initializer involved a private member.  Even if i_default was static, I
could not write:

    ptrTest->f( Test::i_default ) ;

The answer is, I think, that the default value can access private
members.  To quote 8.3.6/5: "The names in the expression are bound, and
the semantic constraints are checked, at the point where the default
argument expression appears."

--
James Kanze    +33 (0)1 39 23 84 71    mailto: kanze@gabi-soft.fr
GABI Software, 22 rue Jacques-Lemercier, 78000 Versailles, France
Conseils en informatique orient   e objet --
              -- Beratung in objektorientierter Datenverarbeitung


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Ron Natalie <ron@sensor.com>
Date: 1998/10/23
Raw View
Christopher Eltschka wrote:

>   void f(int i = i_default) { cout << i << endl; }
> private:
>   int i_default;
>

No, but it has nothing to do with access rights.
Non-static members can not be default arguments.
(Section 8.3.6.9)
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]