[comp.lang.c++] Unwelcomed Standard Conversion

qbarnes@urbana.mcd.mot.com (Quentin Barnes) (04/12/91)

I am new to C++ and am puzzled by a standard conversion.  In Lippman
on p. 160 discussing function matching by standard conversion said
that any numeric type would match any numeric type.  I thought this to
be a bad idea and wrote this to test it:
++
#include <stream.h>

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?
-- 
Quentin Barnes
qbarnes@urbana.mcd.mot.com | ..!uiucuxc!udc!qbarnes

rmartin@clear.com (Bob Martin) (04/19/91)

In article <1991Apr12.153946.21003@urbana.mcd.mot.com> qbarnes@urbana.mcd.mot.com (Quentin Barnes) writes:
>I am new to C++ and am puzzled by a standard conversion.  In Lippman
>on p. 160 discussing function matching by standard conversion said
>that any numeric type would match any numeric type.  I thought this to
>be a bad idea and wrote this to test it:
>++
>#include <stream.h>
>
>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?

I tried your program and got the same result.  A bit frightening.
Then I tried the following variation:

	#include <iostream.h>

	void ff( short v )
	{
		cout << "In short ff " << v << "\n";
	}

	main()
	{
		long i = 70000;
		ff( i );
	}

This complains (with a warning) about truncating the long.`

I think I am going to avoid int declarations where possible.

-- 
+-Robert C. Martin-----+:RRR:::CCC:M:::::M:| Nobody is responsible for |
| rmartin@clear.com    |:R::R:C::::M:M:M:M:| my words but me.  I want  |
| uunet!clrcom!rmartin |:RRR::C::::M::M::M:| all the credit, and all   |
+----------------------+:R::R::CCC:M:::::M:| the blame.  So there.     |

markt@nro.cs.athabascau.ca (Mark Tarrabain) (04/21/91)

qbarnes@urbana.mcd.mot.com (Quentin Barnes) writes:

> ...
> I thought this to be a bad idea and wrote this to test it:
> ++
> #include <stream.h>
> 
> 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 <function 
name>".  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