tony_in_da_uk@yahoo.co.uk Guest
|
Posted: Wed Jul 27, 2005 11:10 pm Post subject: testing object streamability |
|
|
Hi people,
I was just wondering what other people have done when they wanted to
test for the streamability of an object, and if anyone's interested
enough to try my code (attached) - hoping they might report something
about the portability. I've only used it on Solaris with GNU g++ 4.0.1
and Sun CC 5.8.
Thanks in advance,
Tony
[[ Class names changed for my childish amusement. If nobody else has a
better implementation, then full bragging rights reserved ;-P. FWIW,
the problem's a bit harder than testing for other member functions
because operator<<(X&, ...) won't compile. For anyone confused, it's
all about order of matching - much as I hate to refer anyone to an MS
site, useful information does exist at
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccelng/htm/overl_5.asp
]]
namespace Weird
{
// a fallback for when no other operator<< is available
class Donkey;
struct Snake
{
template
Snake(const T&) { }
};
void operator<<(Donkey*, const Snake&)
{
// to get here:
// - Monkey_Stream implicitly converted to Donkey*
// - streamed type implicitly constructed Snake
// - found operator<< by Koenig lookup on stream
// this appears to be just enough obscurity to
// disambiguate other operator<<()s, but just
// enough of a connection to be findable
}
class Monkey_Stream : public std::ostringstream
{
public:
operator Donkey*() { return 0; }
};
}
// Sample usage:
// save(expr) returns the expressions value while
// preserving its streamed representation iff available
class Saver
{
public:
template
const T& save(const T& t)
{
oss_ << t;
return t;
}
string str() const { return oss_.str(); }
private:
Weird::Monkey_Stream oss_;
};
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|