
On 2011-10-15 19:48:07 +0000, Chris Cleeland said:
On Fri, Oct 14, 2011 at 8:37 PM, Kelvin Chung
wrote: I'm trying to specialize boost::lexical_cast to convert Objective-C objects into various standard C++ classes (for example, leveraging NSNumber::-intValue to create lexical_cast<int>(NSNumber*)). However, as Objective-C objects exist as pointers, I would imagine that they would fail the requirements of needing well-defined streaming operator<<() in order to use the stock boost::lexical_cast. Any words of advice on how to do this in a way that will do as little damage as possible?
I haven't looked at the impl for lexical_cast, but this comes to mind off the top of my head...
Perhaps consider a C++ wrapper around the ObjC ptr that knows how to query the ObjC object for its type and turn it into the correct C++ type. Then provide a partial specialization for lexical_cast<> and that type.
Upon another look at this, operator<<() is defined for generic pointers as well as const char* et al., and you'd need to explicitly cast to void* if you wanted to output a const char* as a void*, say. So would a wrapper class be necessary at all? Would just implementing streaming operators, like std::ostream& operator<<(std::ostream& out, id p) { out << [p description]; return out; } std::istream& operator>>(std::istream& in, NSString*& str) { ... } (templates, but you get the point) be all it takes for lexical_cast to work? Or would it be better to just explicitly specialize lexical_cast for specific Objective-C sources and target types? (This comes to play in stuff like the Objective-C class NSNumber, since conversion to and from integral types are well defined but an operator>>(std::basic_istream&, NSNumber*)... not so much)