Topic: function argument initialization
Author: Martin Fabian <fabian@control.chalmers.se>
Date: 1997/11/14 Raw View
Given a function like void f(char *); we all know we can make the call
f("ab");
I claim we should also be able to call the function like
f({'a','b','\0'});
My compiler (MSVC++4.2, btw) does not agree.
Support for my claim:
First, in (at least) two places the standard (or rather the ARM in my case)
explicitly states that argument passing is initialization. "When a function
is called each formal argument is initialized with its actual argument"
(5.2.2, ARM p. 49). "Argument passing and function value return are
initializations" (8.4.3, ARM p. 154). It has to be, otherwise we would not
be able to use references as funtion arguments or returns.
Second, "when an initializer applies to a pointer or an object of
arithmetic type, it consists of a single expression, perhaps in braces"
(8.4, ARM p. 150). This, together with the standard conversion saying that
"an expression with type 'array of T' may be converted to a pointer to the
initial element of the array" (4.6, ARM p. 36), is what allows us to write
things like char *cptr = {'a', 'b' '\0'};. Thus, there is no real
distinction between {'a', 'b' '\0'} as a char * initializer and a char[3]
initializer (see also ARM p. 152-152)
Therefore, in the call to f(char *), f's char * argument is initialized so
that an initializer should be allowed, ergo f({'a','b','\0'}); should be
valid!
What do you think? (I know there are work-arounds, but that's not the point)
Note that the point I'm stressing here applies to ANY type of pointer
argument. Thus, given void g(int *); I claim that the call g({1,2,3});
should also be legal. As should a constructor call such as
Class::Class(const int *); invoked like Class({1,2,3}); which was
originally my problem. Again, this is because we are talking function
argument INITIALIZATION, nothing else.
Martin Fabian, Assistant Professor -------------------------------------
email: fabian@control.chalmers.se | Control Engineering Laboratory
tel: +46 (0)31 772 37 16 | Chalmers University of Technology
fax: +46 (0)31 772 37 30 | S-412 96 Gothenburg
| Sweden
Homepage: http://www.control.chalmers.se/~fabian
------------------------------------------------------------------------
Everyone is talking about real-time, but how real is time, really?
---
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]