Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!sol.ctr.columbia.edu!lll-winken!aunro!ersys!markt From: ersys!markt@nro.cs.athabascau.ca (Mark Tarrabain) Newsgroups: comp.lang.c++ Subject: Re: Unwelcomed Standard Conversion Message-ID: Date: 20 Apr 91 18:29:23 GMT References: <1991Apr12.153946.21003@urbana.mcd.mot.com> Organization: Edmonton Remote Systems, Edmonton, AB, Canada Lines: 54 qbarnes@urbana.mcd.mot.com (Quentin Barnes) writes: > ... > I thought this to be a bad idea and wrote this to test it: > ++ > #include > > void ff( short v ) > { > cout << "In short ff " << v << "\n"; > } > > main() > { > int i = 70000; > ff( i ); > } > ++ > This compiled without warnings with both G++ and cfront 2.0 and silently > truncates the integer. I could not find anything in Ellis about this. > It is certainly not "value-preserving". Bug or feature? > -- The feature which you are referring to is not specific to C++. It was implemented into the ANSI standard for C. When a function is declared in ANSI C, the argument list (and the return value) tell the compiler what sort of values to expect, and to genereate inline conversion to them if neccessary. Much like the following code segment would compile without error or warning: short c; long d; /* and later... */ c=d; Assuming a long is physically larger than a short, data loss is inevitable. If you check an old K&R though, you will find that such 'conversions' are legal and possibly even intentional. As long as such conversions are defined for the two appropriate types, (covering my butt, since we're talking about C++ here, anything could be assigned to anything!) the same conversions will occur with function arguments and return values. There is an exception to this, however. The exception does not apply to C++, since all functions must be declared before use (either by providing a full definition or just a prototype). The exception occurs when (surprise, surprise!) the function is used before it is declared. If there are argument mismatches, you may get an error to the effect of "type mismatch in redeclaration of ". The exact error or warning would depend upon the specific compiler. Prototyping functions before use eliminates the error/warning and produces effects like those mentioned previously. >> Mark Mark Tarrabain ersys!markt@nro.cs.athabascau.ca Edmonton Remote Systems: Serving Northern Alberta since 1982