473,385 Members | 1,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

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
7 1784
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
"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
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
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
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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: James Johnson | last post by:
Are there structs in JavaScript? If not, what's the closest thing, or do I just use parallel arrays? I'm populating a JavaScript array from ColdFusion query, but I don't think I can do this: ...
5
matheussousuke
by: matheussousuke | last post by:
Hello, I'm using tiny MCE plugin on my oscommerce and it is inserting my website URL when I use insert image function in the emails. The goal is: Make it send the email with the URL...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.