[comp.lang.c++] ANSI C & C++ idea

serge@ucbvax.UUCP (02/25/87)

[]
    I would like to propose the following notation (valid only in function
argument declarations/definitions)

	void	f(return Type *t)

to mean that f() will only write the value pointed to by t, and not read it.

    That is,

	void	f(const Type *t)

would mean that f() only reads *t, and does not write it, while

	void	f(return Type *t)

would mean that f() only writes *t, and does not read it, and

	void	f(Type *t)

would mean that f() both reads and writes *t, e.g. t is a
value-result parameter.

    In addition, an argument declared as

	Type *t

could be passed to a function expecting a parameter of type

	const Type *t
or
	return Type *t
or
	Type	*t

    However, an argument declared as

	return Type *t

could only be passed to a function expecting a parameter of type

	return Type *t

and an argument declared as

	const	Type *t

could only be passed to a function expecting a parameter of type

	const	Type *t

    Comments?

						Serge
						serge@ucbvax.berkeley.edu
						...!ucbvax!serge

news@cit-vax.UUCP (02/25/87)

Organization : California Institute of Technology
Keywords: ANSI, C, C++, idea
From: jon@oddhack.Caltech.Edu (Jon Leech)
Path: oddhack!jon

In article <17516@ucbvax.BERKELEY.EDU> serge@ucbvax.BERKELEY.EDU (serge) writes:
>[]
>    I would like to propose the following notation (valid only in function
>argument declarations/definitions)
>
>	void	f(return Type *t)
>
>to mean that f() will only write the value pointed to by t, and not read it.

	What's your motivation for doing this? I.e. what kind of 
optimizations can a compiler make based solely on knowing that a variable 
is 'write-only'? 

	One difference I see from 'const' is that the 'const' attribute
may actually be enforced by the hardware as opposed to being a semantic 
abstraction. Perhaps MMUs that enforce write-only memory exist (the VAX
doesn't seem to based on a quick perusal of the architecture handbook), 
but what do you use it for?

    -- Jon Leech (jon@csvax.caltech.edu || ...seismo!cit-vax!jon)
    Caltech Computer Science Graphics Group
    __@/

serge@ucbvax.UUCP (02/27/87)

In article <1857@cit-vax.Caltech.Edu> jon@oddhack.UUCP (Jon Leech) writes:
>In article <17516@ucbvax.BERKELEY.EDU> serge@ucbvax.BERKELEY.EDU (serge) writes:
>>    I would like to propose the following notation (valid only in function
>>argument declarations/definitions)
>>
>>	void	f(return Type *t)
>>
>>to mean that f() will only write the value pointed to by t, and not read it.
>
>	What's your motivation for doing this? I.e. what kind of 
>optimizations can a compiler make based solely on knowing that a variable 
>is 'write-only'? 
>
>	One difference I see from 'const' is that the 'const' attribute
>may actually be enforced by the hardware as opposed to being a semantic 
>abstraction. Perhaps MMUs that enforce write-only memory exist (the VAX
>doesn't seem to based on a quick perusal of the architecture handbook), 
>but what do you use it for?

    The motivation is to provide better type/error checking.  For
example, one of the more common mistakes in UN*X programming is to pass
uninitialized pointers to functions that expect valid addresses, e.g
doing (incorrectly)

	struct	stat	*s;

	stat("file", s);

instead of

	struct	stat	s;

	stat("file", &s);

because the man page declares stat() as

	extern	int	stat(const char *file, struct stat *buf);

    By using the return modifier these kinds of errors can be avoided,
by having the compiler make sure that a valid address is passed to a
function declared as

	extern	int	stat(const char *file, return struct stat *buf);

and that a parameter declared as

	return Type *t
or
	Type *t

is written to (a value is assigned to *t) before the function returns,
just as a parameter declared as

	const Type *t

is not written to (no assignment to *t occurs).

						Serge
						serge@ucbvax.berkeley.edu
						...!ucbvax!serge

P.S.	Note that in C++, the following declaration

		extern	void	f(return Type& t)

	would also be valid.