Conversion operator not being picked up by compiler
Conversion operator not being picked up by compiler
- Subject: Conversion operator not being picked up by compiler
- From: "Greg Norz" <email@hidden>
- Date: Fri, 13 Jan 2006 13:03:28 -0600
- Thread-topic: Conversion operator not being picked up by compiler
Below is a VERY contrived example of some code that's generating
compiler warnings. We happen to turn on all warnings and treat them as
errors, so the code HAS to be fixed. What happens is that a classes
conversion operator is not being called (or at least the compiler can't
figure it out) unless a static_cast is placed in front of the offending
variable. Consider :
--- BEGIN ---
#include <iostream>
class Foo
{
public :
explicit Foo( const char* message ) : m_str( message ) {}
operator const char*() { return m_str.c_str(); }
private :
std::string m_str;
};
void Write( const char* msg, ... )
{
std::cout << msg << std::endl;
}
int main( int argc, char* const argv[] )
{
Foo foo( "Hello, World!" );
Write( "%s", foo );
return 0;
}
--- END ---
The compiler spits out a warning on the line calling "Write()" saying
"cannot pass objects of non-POD type 'class Foo' through '...'; call
will abort at runtime." This is understandable except for the fact that
the conversion operator should be recognized by the compiler. (well, in
my opinion at least!)
If I substitute the following : "Write( "%s", static_cast<const
char*>(foo) );", then the compiler carries on and finishes successfully.
This isn't the first time I've had a problem with Xcode finding a
conversion operator, and it makes the construction of generic code hard
when you end up casting everything. Has anyone found this to be a
problem as well? Any solutions besides placing static_cast operators
throughout our code?
I haven't dug too deeply into the C++ standard, so maybe the compiler
warning is valid and other platforms just suck up the error and/or make
an assumption based on the conversion operator.
greg norz
southwest airlines || southwest.com
email@hidden <mailto:email@hidden>
Wright is Wrong! - www.setlovefree.com <http://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