Re: [Cppunit-devel] templatized assertions
Brought to you by:
blep
From: Steve M. R. <ste...@vi...> - 2001-05-19 16:38:09
|
Hi Baptiste, First, thanks _so_ much for suggesting that an equality function be part of the assertion traits class. That's precisely what I needed yesterday! The brilliant point is that both equality and the string representation for a type can be different for assertions than it is for other code. The traits class I'm using looks like the following, which can easily be specialized. namespace CppUnit { template <class T> struct assertion_traits { static bool equal( const T& x, const T& y ) { return x == y; } static std::string toString( const T& x ) { ostringstream ost; ost << x; return ost.str(); } }; } On Sat, May 19, 2001 at 02:38:07PM +0200, Baptiste Lepilleur wrote: > > > > On Wed, May 16, 2001 at 09:28:06PM +0200, Baptiste Lepilleur wrote: > > > I would use a trait to add the object to the stream. The trait generic > > > implementation would use '<<', but could be specialized when needed. > > > > That sounds like a reasonable idea. I'm not terribly experienced in > > traits; what would be a good name for the function? Some of CppUnit > > already uses "toString", which seems reasonable enough, yes? > > How about using specialized template function: [...] > I like that one because it solves the naming problem and you can change only > the behavior you really need to change. That sounds like you want to do away with wrapping the functions in a common traits class. I see your point about being able to override each function individually. I'm not convinced that this outweighs the value of wrapping up the functions in a common name space, as a traits class does. I guess you could use a "traits namespace", instead. I'm not well-versed enough to know why classes are used over namespaces for traits. Maybe its historical? Or maybe it has something to do with being able to partially specialize a class template? I discovered the hard way that GCC does not allow default template parameters in templated functions, while it does allow them for templated classes. This leads me to suspect that templated classes are better supported than templated functions, and for this reason the former are used for traits. Comments? -Steve -- by Rocket to the Moon, by Airplane to the Rocket, by Taxi to the Airport, by Frontdoor to the Taxi, by throwing back the blanket and laying down the legs ... - They Might Be Giants |