[comp.lang.c++] Implicit argument coercion considered harmful

gdykes@batcomputer.tn.cornell.edu (Gene Dykes) (01/05/88)

Most people seem to think that implicit coercion of arguments to C++ functions
is the greatest thing since sliced bread.  Personally, I consider it a bug,
not a feature.  Consider the following example:

I have created a function that is supposed to accept three floating point
color values (red, green, and blue) in the range 0.0 to 1.0.  Now along
comes somebody who is used to specifying his colors as integers in the range
0 to 255.  I am fond of telling people that one of the main advantages of using
C++ is that it catches badly typed arguments at compile time, but C++ insists
on coercing integers to floats in cases like this, even when it is clearly a
bad idea to do so.  Ah, I can hear you gurus claiming that I should have
overloaded the function to accept either common color convention.  Yes, that
solves this trivial example, but suppose I have a function with lots of
arguments where integers and floats might be confused?  Am I supposed to 
write a function for every possible permutation?  This could easily be tens
or hundreds of functions.

A more important example:

We have a large graphics package supplied by our vendor.  It does great
graphics, but its calling parameters are greatly lacking in clarity.
Would you know what this function call does without looking at a manual:

	hidden_surface (channel, TRUE, FALSE) ;

What I want to do is require our programmers to use macros that are
self-documenting:

	hidden_surface (channel, ENABLE, CULLING_OFF) ;

But implicit coercion of arguments completely undermines this possibility.

I think it would be ideal if there was some kind of control over whether
or not arguments would be coerced or not.  What I would really like to
do is have this in a header:

	typedef const int calling_arg ;
	calling_arg ENABLE = TRUE ;
	calling_arg CULLING_OFF = FALSE ;
	void hidden_surface (int, calling_arg, calling_arg) ;

As it stands now, no matter what calling_arg is typedef'd to be,
FALSE (0) can be coerced into it.
-- 
Gene Dykes, 120 Rand Hall, Cornell U., Ithaca, NY 14853 (607)255-6713
UUCP: {uw-beaver,ihnp4,decvax,allegra,vax135}!cornell!batcomputer!gdykes
ARPA: gdykes@tcgould.tn.cornell.edu
BITNET: gdykes@CRNLTHRY

jss@hector.UUCP (Jerry Schwarz) (01/06/88)

In article <3275@batcomputer.tn.cornell.edu> gdykes@batcomputer.tn.cornell.edu (Gene Dykes) writes:
>
>Most people seem to think that implicit coercion of arguments to C++ functions
>is the greatest thing since sliced bread.  Personally, I consider it a bug,
>not a feature.  Consider the following example:
>
>I have created a function that is supposed to accept three floating point
>color values (red, green, and blue) in the range 0.0 to 1.0.  Now along
>comes somebody who is used to specifying his colors as integers in the range
>0 to 255.  I am fond of telling people that one of the main advantages of using
>C++ is that it catches badly typed arguments at compile time, but C++ insists
>on coercing integers to floats in cases like this, even when it is clearly a
>bad idea to do so.  Ah, I can hear you gurus claiming that I should have
>overloaded the function to accept either common color convention.  Yes, that
>solves this trivial example, but suppose I have a function with lots of
>arguments where integers and floats might be confused?  Am I supposed to 
>write a function for every possible permutation?  This could easily be tens
>or hundreds of functions.

Perhaps you should consider really using the C++ typing facility
rather than trying to get by with just the simple C types.
In particular, invent a class ColorValue.  

	class ColorValue {
	public:
		ColorValue(int) ;
		ColorValue(float) ;
		operator float() ;
	private: ...
		} ;

Now type the arguments to your function as ColorValues and the 
implicit coercion of arguments will pretty much do what you want.  

Once you start thinking along these lines many other possibilities
may occur to you.  

Jerry Schwarz