* arnuld:
problem: define functions F(char), g(char&) & h(const char&). call
them with arguments 'a', 49, 3300, c, uc & sc where c is a char, uc is
unsigned char & sc is signed char. whihc calls are legal? which calls
cause the compiler to to introduce a temporary variable?
solution: this is the code
-----------------------------------------------------------
#include <iostream>
void f(char) {};
void g(char&){};
void h(const char&) {};
Semicolons. I'm not sure whether they're just superfluous or
syntactically incorrect.
int main() {
char c;
unsigned char uc;
signed char sc;
f(c);
f(uc);
f(sc);
f('a');
f(49);
f(3300);
}
-----------------------------------------------------------
& the error is:
------------------------------------------------------------------------------
[arnuld@localhost cpp]$ g++ 05_ex-06.cc
05_ex-06.cc: In function 'int main()':
05_ex-06.cc:20: warning: overflow in implicit constant conversion
[arnuld@localhost cpp]$
---------------------------------------------------------------------------------
No, at the technical level this is just a warning (although such a
warning typically indicates that the programmer has intended something
else than the code actually expresses). Apart from possibly the
semicolons, the code above is technically valid, all calls "legal". If
you do the same for g and h, however, you'll then have calls that the
compiler should not accept.
"f" accepts "char" & i know 'char' belongs to 'integeral types' as
'char' will be converted to an 'int'. it means 'f(49)' will be called
with whatever character 49 presents in ASCII table. am i right?
In some coded character set, yes. If that's ASCII or some ASCII
extension on your computer, it will be ASCII.
2nd, the error belongs to 'f(3300)' as 3300 is larger than one byte &
compiler expected an 'int' not larger than the size of 'char'. right?
No, although in practice that will often signify a /logical error/, that
the program does something else than intended.
The result depends on the signedness of 'char' for your implementation
and compiler switches.
Conversion of an integral value v of any type to unsigned type T is
defined by the standard; the result is v mod 2^n where n is the number
of value representation bits in T. Conversion to signed T is
implementation defined if the value v is not representable as a T.
---
To answer Stroustrup's question about which calls are "legal", all you
have to do is to try the same with g and h, assuming a reasonably
standard-conforming compiler. But of course it's a good idea to also
understand why. To answer Stroustrup's question about which calls
introduce a temporary variable, you have to analyze things, essentially,
answering the question "why would a temporary variable be /necessary/".
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?