473,387 Members | 1,579 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,387 software developers and data experts.

Conversion operators and ambiguity

If I were to include in the definition of a string class the following
operators:

class String {
public:
// ...
operator const char *() const; // conversion to C-style string
char operator[](size_t i) const; // character indexing
// ...
};

I can't use the indexing operator because the compiler doesn't know whether
I mean to index the String object directly or to convert to a const char *
and then index on that. It is clear to me, at least in this case, that
having gone to the trouble of defining an indexing operator for the class,
it is that function I want to call. I would have thought that this would be
obvious to any compiler too - only attempt to perform conversions if an
expression doesn't make sense without them.

Could anyone explain to me why C++ doesn't behave like this? I can't think
of any example where this would be undesirable (although I could believe
they might exist), and it seems that it would be very useful, particularly
when interfacing with C code which expects more primitive types.

Richard
Jul 23 '05 #1
2 2206

rasbury wrote:
If I were to include in the definition of a string class the following operators:

class String {
public:
// ...
operator const char *() const; // conversion to C-style string char operator[](size_t i) const; // character indexing
// ...
};

I can't use the indexing operator because the compiler doesn't know whether I mean to index the String object directly or to convert to a const char * and then index on that. It is clear to me, at least in this case, that having gone to the trouble of defining an indexing operator for the class, it is that function I want to call. I would have thought that this would be obvious to any compiler too - only attempt to perform conversions if an expression doesn't make sense without them.

Could anyone explain to me why C++ doesn't behave like this? I can't think of any example where this would be undesirable (although I could believe they might exist), and it seems that it would be very useful, particularly when interfacing with C code which expects more primitive types.

Richard


I tried your code with the Comeau compiler
(http://www.comeaucomputing.com/tryitout/). It compiles fine. You are
probably using a buggy compiler.

Hope this helps,
-shez-

Jul 23 '05 #2
"rasbury" <di**********************@hotmail.com> wrote in message
news:cu**********@sparta.btinternet.com...
If I were to include in the definition of a string class the following
operators:

class String {
public:
// ...
operator const char *() const; // conversion to C-style string
char operator[](size_t i) const; // character indexing
// ...
};

I can't use the indexing operator because the compiler doesn't know whether I mean to index the String object directly or to convert to a const char *
and then index on that. It is clear to me, at least in this case, that
having gone to the trouble of defining an indexing operator for the class,
it is that function I want to call. I would have thought that this would be obvious to any compiler too - only attempt to perform conversions if an
expression doesn't make sense without them.

Could anyone explain to me why C++ doesn't behave like this? I can't think
of any example where this would be undesirable (although I could believe
they might exist), and it seems that it would be very useful, particularly
when interfacing with C code which expects more primitive types.


As was said before, the compiler shouldn't consider anything ambiguous when
you invoke operator[], so it sounds like your compiler has a bug.

My suggestion would be to do something similar to std::string's approach and
avoid an implicit conversion, preferring to use a member function that
resembles std::string::c_str(). You'd have to type a few extra characters
to convert it to a const char *, but being explicit about the conversion can
help prevent accidental bugs.

--
David Hilsee
Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Philip Pemberton | last post by:
Hi, I've just been trying to get some (pretty badly written) code working on a different compiler. Unfortunately, I've hit a problem. When the code below is compiled under Borland C++ it...
3
by: BigMan | last post by:
Here is a piece of code: #include <memory> using namespace std; template< typename SomeType > void f(auto_ptr_ref< SomeType >) { }
2
by: rasbury | last post by:
If I were to include in the definition of a string class the following operators: class String { public: // ... operator const char *() const; // conversion to C-style string char...
5
by: Vijai Kalyan | last post by:
Hello, I have come back to C++ after a couple of years with Java so I am quite rusty and this question may seem poor: My platform is Windows XP with MSVC 7.1. I have a class with a...
6
by: Arne Schmitz | last post by:
I guess this has been asked before, but I cannot find any answer to this problem. I have program like this: ---SNIP--- #include <cassert> #include <cstdlib> class C { public:
17
by: Steve R. Hastings | last post by:
I have been studying Python recently, and I read a comment on one web page that said something like "the people using Python for heavy math really wish they could define their own operators". The...
1
by: hunter hou | last post by:
Hello,Please look at the following code(from C++ in a nutshell) and my questions.Thanks,***Hunter... typedef void (*strproc)(const char*); void print(const char* str) { std::cout << "const...
14
by: Richard G. Riley | last post by:
Would it be wrong to use "implicit casting" instead of the standards "implicit conversion" when talking about implicit conversions between certain data types. The standard mentions "explicit...
4
by: subramanian100in | last post by:
In the book, C++ Coding Standards book by Hereb Sutter and Andrei Alexandrescu, in Item 40 on pages 86-87 viz, "Avoid providing implicit conversions", the authors have advised the use of named...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.