By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,134 Members | 1,796 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,134 IT Pros & Developers. It's quick & easy.

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

P: n/a
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::operator 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...
--
Philippe Guglielmetti - www.dynabits.com
Jul 22 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Philippe Guglielmetti wrote:

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::operator 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.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #2

P: n/a
"Karl Heinz Buchegger" wrote:
std::string bad='x'+"yz"; // results in anything except "xyz"
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.


right... My idea was rather to prevent char arithmetic...
--
Philippe Guglielmetti - www.dynabits.com
Jul 22 '05 #3

P: n/a
Philippe Guglielmetti wrote:

"Karl Heinz Buchegger" wrote:
std::string bad='x'+"yz"; // results in anything except "xyz"
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.


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']

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #4

P: n/a
Philippe Guglielmetti wrote:
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::operator char*() conversion...)

Any idea on how to prevent this to compile ?


Not using automatic conversions to char * in any class.

--
Salu2
Jul 22 '05 #5

P: n/a
Philippe Guglielmetti wrote:
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::operator 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.

--
Best regards,
Andrey Tarasevich
Jul 22 '05 #6

P: n/a
Philippe Guglielmetti wrote:
"Karl Heinz Buchegger" wrote:
> std::string bad='x'+"yz"; // results in anything except "xyz"
> 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.


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.
Jul 22 '05 #7

P: n/a
"Philippe Guglielmetti" <ne**@dynabits.com> wrote:
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::operator char*() conversion...)

Any idea on how to prevent this to compile ?


Don't use classes with operator type() conversions.
Jul 22 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.