RE: Conversion operator not being picked up by compiler
RE: Conversion operator not being picked up by compiler
- Subject: RE: Conversion operator not being picked up by compiler
- From: "Greg Norz" <email@hidden>
- Date: Fri, 13 Jan 2006 14:58:47 -0600
- Thread-topic: Conversion operator not being picked up by compiler
| Hi,
| (rushed answer:)
| I don't *think* code is not well-formed because foo
| (type Foo) is passed to a variadic (...) function parameter.
| The compiler can't deduce from your format string "%s" that a
| 'const char*' is sought, and thus doesn't know to look for an
| implicit-conversion.
Well, in my opinion, it shouldn't matter that I HAPPEN to want a 'const
char*'. To me, the only concern is that the compiler wants a POD type,
and I'm giving it a POD type via a conversion operator. Let's say that I
modify 'class Foo' to convert to different types of data, such that I
have 'operator int()' and 'operator wchar_t*()' and 'operator double()'.
All of those are POD types and the compiler should know enough to check
an object for those types of conversions. I guess in that case, though,
you would NEED a 'static_cast<>' because the compiler may not be smart
enough to know which conversion operator you really want. I would
totally understand that error, and a cast would be totally warranted to
inform the compiler as to the true intentions of the code.
Also, consider when you perform a standard 'printf' :
const char* someStr = "Hello";
const long someNum = 42;
printf( "Some number = %d, some string = %s", someStr, someNum );
Unless you have some special compiler type checking enabled (which I
think Xcode has), the compiler won't warn you that you've mixed up your
types in the varargs portion of 'printf'. All the compiler cares about
at that point is the fact that you gave it POD types that it can easily
work with.
It just seems to me that gcc is not looking at objects to see if they
have conversions to a POD type. This code works on Visual C++ 7.1, and
my guess there is that they don't do any checking whatsoever. In fact,
I'm 99% sure that's the case because I've caught errors in the past
where I pass a 'std::string' into a '*printf' function and received the
address of the variable.
| Quick and dirty workarounds: (aside from static_cast<...>)
| * write an accessor to Foo that returns const string& ::m_str,
But that's what the conversion operator is -- an accessor that's
supposed to be automatic.
| * overload by adding: (akin to specialization)
| void Write(const char* fmt, const Foo& f) {
| Write(fmt, /* get string from f */ );
| }
| The compiler will find this as the best match over
| the variadic version.
Not possible because I might want to have more than just 'Foo' in the
varargs list. Like so :
Write( "%s - %d - %s - %d", Foo(someStr), someNum, "Hi, there",
Foo(someNum) );
The static_cast<> is fine by me. It at least compiles the code and
appears to allow it to run correctly. I just think that gcc should be
checking conversion operators in the future before saying that code will
abort at runtime. Unless, of course, I'm using the conversion operators
incorrectly in my example, in which case I stand corrected! :^)
One final comment : I have some other compilers at home (CodeWarrior,
Intel C++ for Windows) that I might try to see how they handle it. It
would be interesting to compare the results.
Oh, and in an additional post you made the comment that the code would
most likely print only "%s" when run. I agree, but didn't feel like
writing all of the vararg code in there to handle it properly! :^)
Thanks to all who have replied.
greg norz
southwest airlines || southwest.com
email@hidden
Wright is Wrong! - www.setlovefree.com
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden