Re: [Cppunit-devel] equality of const objects
Brought to you by:
blep
From: Duane M. <du...@us...> - 2001-10-01 19:46:03
|
> >Can't we have both? What about > > template <class T> > void assertEquals( const T& expected, > const T& actual, ... ) > > template <class T> > void assertEquals( T& expected, > T& actual, ... ) > >with the same implementation. >[Ditto for the default assertion_traits template] > >I just tried this modification; the library builds and runs >the test suite successfully. Comments? This is perfectly legal C++ (overloads by const). (If Forte doesnt support this, how does it compile the C++ standard library; there are several places where this overload is used.) I would also make the argument (FWIW) that const is all that is needed. Comparison operators should not be mutating. (Caches are caches and should be marked mutable.) This is also where you want to make the change for comparing equivalent objects (not the same type). The change would be thus: template <class TExepect, TActual> void assertEquals( const TExepect & expected, const TActual & actual, ... ) The compiler will complain if the objects cannot be compared. I ran into this problem with my tests. Consider: enum { noErr = 0, someErr = -100 }; int FunctionToTest(); .. CPPUNIT_ASSERT_EQUAL( noErr, FunctionToTest() ); This wont compile because noErr is an anonymous enum type and not an int. The problem with the above change to assertEquals is that it is embedded in a template class that also defines the toString operations on one type. This has to be solved also. A couple of suggestions: (1) seperate the class into two template classes (2) Dont use a template class just template functions (3) Use class template methods (I have the terminology wrong for sure). There may be a limitation with some compilers using this method. Anyhow, just some suggestions. .Duane |