Topic: Defaults using other params; & Def/init of static data members


Author: Geir.Halbo@sigyn.idt.unit.no (Geir Halbo)
Date: 5 Apr 93 05:37:15
Raw View
To you gurus in C++ history, language def. and compilers out there,


I experienced the error message

  "Default expression may not use local variables"

when trying to define

  Send( char *s ="", int len =strlen(s) )  { ...

(My compiler is Borland C++ 3.1.)

I understand the message, but would it not be possible to allow the
use of other parameters in default expressions?  My meaning is obvious
(if len is not given, calculate the len parameter from the string
parameter), and there should be no problem implementing this feature
in the compiler, or what?


And:

I have now learned that

  struct x { static int y; };
  int x::y = 1;

is how static members are defined, whereas

  struct x { static int y = 1; };

Is there any good reason why inline definitions of static class
members are illegal?  This limitation never fails to make my code less
readable and more complicated than needed.


Maybe these remarks could be taken into consideration in the work to
standardize C++?
Replies appreciated.  Thanks.

 - geir

--

Geir Halbo         !  Dept. of CS and telematics (IDT)
Halbo@idt.unit.no  !  Norwegian Inst. of Techn.  (NTH)
Direct response by email appreciated.




Author: fjh@munta.cs.mu.OZ.AU (Fergus James HENDERSON)
Date: Mon, 5 Apr 1993 05:32:18 GMT
Raw View
Geir.Halbo@sigyn.idt.unit.no (Geir Halbo) writes:

>I have now learned that
>
>  struct x { static int y; };
>  int x::y = 1;
>
>is how static members are defined, whereas
>
>  struct x { static int y = 1; };
>
>Is there any good reason why inline definitions of static class
>members are illegal?  This limitation never fails to make my code less
>readable and more complicated than needed.

There is a reason: to make it easier for language implementors.

If the same declaration
 struct x { static int y = 1; };
occurs in two different compilation units (.c files), then the compiler
will have difficulty deciding which object file should contain the
definition for x::y. Note that this problem doesn't arise for inline
definitions of member functions, since the compiler can safely produce
multiple copies of inline functions, but this easy (though inefficient)
solution won't work for inline definitions of static variables.

Whether this reason is good enough is however debatable -
there is currently a proposal (written by John Skaller) that is
being considered by the extensions subcommittee to make this code
legal.

--
Fergus Henderson             fjh@munta.cs.mu.OZ.AU
This .signature virus is a self-referential statement that is true - but
you will only be able to consistently believe it if you copy it to your own
.signature file!




Author: bs@alice.att.com (Bjarne Stroustrup)
Date: 5 Apr 93 20:13:45 GMT
Raw View


Geir.Halbo@sigyn.idt.unit.no (Geir Halbo @ Div. of CS & Telematics, Norwegian Institute of Technology) writes

 > I experienced the error message
 >
 >   "Default expression may not use local variables"
 >
 > when trying to define
 >
 >   Send( char *s ="", int len =strlen(s) )  { ...
 >
 > (My compiler is Borland C++ 3.1.)
 >
 > I understand the message, but would it not be possible to allow the
 > use of other parameters in default expressions?  My meaning is obvious
 > (if len is not given, calculate the len parameter from the string
 > parameter), and there should be no problem implementing this feature
 > in the compiler, or what?

The snag is that the order of argument evaluation is implementation defined
in C and in C++. What would happen if the `len' argument is evaluated before
the `s' argument? Some compilers do that.