implicit type conversion

J

jschon

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
 
I

Ivan Vecerina

: 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top