470,571 Members | 2,440 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,571 developers. It's quick & easy.

implicit type conversion

How do I compile with g++ version 4.0 so that a int will not be
implcitly converted to a string.

for example...i would like the following code to result in an error

int x = 4;
string cat;
cat = x;
thank you,
jake

Aug 4 '06 #1
1 1752
<js****@umich.eduwrote in message
news:11**********************@p79g2000cwp.googlegr oups.com...
: How do I compile with g++ version 4.0 so that a int will not be
: implcitly converted to a string.
There isn't actually such an implicit conversion, but ...

: for example...i would like the following code to result in an error
:
: int x = 4;
: string cat;
: cat = x;
This unfortunately "works" because std::string has
an assignment operator that accepts a single char.
So the last line is actually equivalent to:
cat.operator=( (char)x );

So the real problem here (other than the unfortunate
decision to provide this assignment op in the std lib)
is the implicit conversion from int to char .

Most compilers provide an option to issue a warning
when an implicit conversion results in the truncation
of an integer type. Please look up in your compiler's
doc, or as in a g++-dedicated forum (I don't have the
info for g++ here...)

hth -Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <http://www.brainbench.com


Aug 4 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

15 posts views Thread by buda | last post: by
3 posts views Thread by /* frank */ | last post: by
11 posts views Thread by Steve Gough | last post: by
9 posts views Thread by Girish | last post: by
36 posts views Thread by Chad Z. Hower aka Kudzu | last post: by
1 post views Thread by Joe HM | last post: by
3 posts views Thread by Jess | last post: by
8 posts views Thread by jonpb | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.