any way to prevent 'x'+"yz" to compile ?

  • Thread starter Philippe Guglielmetti
  • Start date
P

Philippe Guglielmetti

I spent hours chasing a stupid bug of this kind:
std::string bad='x'+"yz"; // results in anything except "xyz"
("yz" was in fact returned by some class::eek:perator char*() conversion...)

Any idea on how to prevent this to compile ?

I tried to define an operator+(const char lhs, const char* rhs) that would
fail, but my compiler (VC 7.1) says one of the operands must be a class...
 
K

Karl Heinz Buchegger

Philippe said:
I spent hours chasing a stupid bug of this kind:
std::string bad='x'+"yz"; // results in anything except "xyz"
("yz" was in fact returned by some class::eek:perator char*() conversion...)

Any idea on how to prevent this to compile ?

I don't think this is possible.
Preventing this from compilation would be equivalent
to turning off pointer arithmetic.
 
P

Philippe Guglielmetti

Karl Heinz Buchegger said:
I don't think this is possible.
Preventing this from compilation would be equivalent
to turning off pointer arithmetic.

right... My idea was rather to prevent char arithmetic...
 
K

Karl Heinz Buchegger

Philippe said:
right... My idea was rather to prevent char arithmetic...
( You can't redefine the operations on built in types. )

:)
But the above *is* pointer arithmetic

The result is a pointer which points 'x' (on an ASCII
System that would be 120) characters behind the beginning
of "yz"

'x'+"yz" == & "yz"['x']
 
?

=?ISO-8859-15?Q?Juli=E1n?= Albo

Philippe said:
I spent hours chasing a stupid bug of this kind:
std::string bad='x'+"yz"; // results in anything except "xyz"
("yz" was in fact returned by some class::eek:perator char*() conversion...)

Any idea on how to prevent this to compile ?

Not using automatic conversions to char * in any class.
 
A

Andrey Tarasevich

Philippe said:
I spent hours chasing a stupid bug of this kind:
std::string bad='x'+"yz"; // results in anything except "xyz"
("yz" was in fact returned by some class::eek:perator char*() conversion...)

Any idea on how to prevent this to compile ?

I tried to define an operator+(const char lhs, const char* rhs) that would
fail, but my compiler (VC 7.1) says one of the operands must be a class...

Since one of the operands was actually a class, you can try to declare
an 'operator+(const char lhs, const your_class& rhs)'. Overload
resolution will choose this operator, instead of going through the
conversion to 'char*'. If you intentionally "forget" to provide a
definition for this operator, such errors will be detected at compile time.
 
R

Rolf Magnus

Philippe said:
right... My idea was rather to prevent char arithmetic...

Well, char is just an integer type, so what you're doing is adding an
integer to a pointer, which is just what pointer arithmetic is. You
cannot write your own operators with only built-in types as arguments,
because that might have some unexpected results. You're not allowed to
alter the behaviour of C++'s built-in types.
 
O

Old Wolf

Philippe Guglielmetti said:
I spent hours chasing a stupid bug of this kind:
std::string bad='x'+"yz"; // results in anything except "xyz"
("yz" was in fact returned by some class::eek:perator char*() conversion...)

Any idea on how to prevent this to compile ?

Don't use classes with operator type() conversions.
 

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

Forum statistics

Threads
474,175
Messages
2,570,942
Members
47,490
Latest member
Finplus

Latest Threads

Top