Path: utzoo!mnetor!uunet!oddjob!hao!gatech!udel!rochester!cornell!batcomputer!gdykes From: gdykes@batcomputer.tn.cornell.edu (Gene Dykes) Newsgroups: comp.lang.c++ Subject: Implicit argument coercion considered harmful Message-ID: <3275@batcomputer.tn.cornell.edu> Date: 4 Jan 88 17:13:12 GMT Distribution: comp Organization: Theory Center, Cornell U., Ithaca NY Lines: 49 Keywords: coercion 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