Topic: Clarification of default-parameters
Author: schuenem@Informatik.TU-Muenchen.DE (Ulf Schuenemann)
Date: 15 Apr 1994 12:19:05 GMT Raw View
Hello,
Who knows more about default parameters? I do not know the wording of the
current standard, nor do I know of any considerations of the committee about
default-parameters. I mainly got my knowledge by experimenting with gcc
version 2.4.3. Please correct me if I got something wrong.
( I ) 'x=exp'
( II ) 'x=X(exp)'
HOW TO CALL CONSTRUCTORS WITH MORE THAN 1 ARGUMENT /
DEFAULT-CONSTRUCTORS (= CTOR WITH 0 ARGUMENTS) ?
( III ) 'x=exp' 'x=X(exp)' and 'x(exp)'
( IV ) 'x=(exp)' GCC-BUG ?
--------------------------------------------------------------------
( I ) 'a=exp'
It is possible to supply (from right to left) default-parameters for
function-calls. Any expression can be given as default-param, it
is evaluated every time the function is called.
struct X { int v;
X(int i) { v=i; printf("called X(%d)\n",i); }
X(X& x) { v=x.v; printf("called X(x.%d)\n",x.v); }
} xx (-1);
char CC=3;
void f( X a=printf("using default args for g\n")
, X b=2+4*5, X c=CC, X d=xx);
>called X(-1)
>using default args for g
>called X(25)
>called X(22)
>called X(3)
>called X(x.-1)
--------------------------------------------------------------------
( II ) 'x=X(exp)'
HOW TO CALL CONSTRUCTORS WITH MORE THAN 1 ARGUMENT /
DEFAULT-CONSTRUCTORS (= CTOR WITH 0 ARGUMENTS) ?
I found out that 'X x=X()' can be used to supply the default-ctor as
default-param and 'XY xy=XY(1,2)' can be used to supply ctors with 2 args.
When I look at it my first thought is:
"a X/XY is contructed and than this is used as argument to copy-ctor-call
to contruct x/xy"
But I am wrong, x/xy are constructed directly. 'X x=X();' 'X x=X(1);'
'X x=X(1,2);' etc are idioms I can also use in var-declarations, being the
same as 'X x;' 'X x(1)' 'X x(1,2)'
So 'f(X x=1)' is the same as 'f(X x=X(1))'.
My question: Is this official syntax?
--------------------------------------------------------------------
( III ) 'x=exp' 'x=X(exp)' and 'x(exp)'
I thought 'X x(1)' is always the C++ form of C's form ('X x=1') of the
constructor-call with 1 arg.
If so, 'f(X x(1))' (*) should be the same as
'f(X x=1)'
and 'f(X x=X(1))'
and also 'f(X x(1,2))' (**) should be the same as
'f(X x=X(1,2)'
Well, there seems to be no solution how to express 'f(X x=X())' this way.
However gcc does not accept (*) and (**) (see IV).
Have (*) and (**) been discussed by the committee? Are they allowed?
--------------------------------------------------------------------
( IV ) 'x(exp)' GCC-BUG ?
When I tried to use 'X x(exp)' instead of 'X x=exp' with gcc:
void f( X a(printf("using default args for g\n"))
, X b(2+4*5), X c(CC), X d(xx));
then a, b, c and d are constructed before main() like static vars of f().
And if I try to call 'f( xx );' or 'f( X(1) );' gcc flags an error:
>too many arguments to function `void f ()'
And gcc believes that
void f( X a=printf("using default args for g\n")
^
, X b(2+4*5), X c(CC), X d=xx);
^ ^ ^ ^ ^
has the type:
`void f (struct X (= ( printf ((const char *)((char *)
"using default args for g\012"))))
, struct X (= xx ))'
I suppose this is a bug. If have no idea how gcc could come to this
result in a correct way. Or have I missed some new C++ features?
Thanks for your help,
Ulf Schuenemann
--------------------------------------------------------------------
Ulf Sch nemann
Institut f r Informatik, Technische Universit t M nchen.
email: schuenem@informatik.tu-muenchen.de
WWW: http://hphalle2/~schuenem (currently not available from outside)