Costness of MatcherMethod<PtrT*>
Description
When creating a custom Matcher for a signed char* type, I came across this error message.
catch.hpp: In instantiation of ‘void Catch::ResultBuilder::captureMatch(const ArgT&, const MatcherT&, const char*) [with ArgT = signed char [4]; MatcherT = EqualsMatcher
]’: xxx_test.cpp: required from here catch.hpp:1964:9: error: invalid conversion from ‘const signed char*’ to ‘signed char*’ [-fpermissive] setResultType( matcher.match( arg ) ); ^~~~~~~~~~~~~ xxx_test.cpp: note: initializing argument 1 of ‘bool EqualsMatcher ::match(T*) const [with T = signed char]’ bool match(T* other) const override { ^~~~~
It seems that the match method of MatherMethod<PtrT*> should be const. Like
bool match( PtrT const* arg ) const = 0;
Extra information
- Catch version: v1.9.4
- Operating System: Windows 10 - Professional
- Compiler+version: Cygwin - GCC 6.3.0
How are you deriving from MatcherBase?
I was trying to creating an c-array equals matcher like so
template<typename T>
class EqualsCArrayMatcher : public Catch::MatcherBase<T*> {
public:
EqualsCArrayMatcher (const T* comparator, std::size_t length)
: comparator_(comparator), length_(length) {}
bool match(T* other) const override {
for (size_t i = 0; i < this->length_; i++) {
if (this->comparator_[i] != other[i]) {
return false;
}
}
return true;
}
...
};
But for an c-array of signed char's it failed.
Try deriving from Catch::MatcherBase<T const*>
and change match to:
bool match(T const* other) const override
This seems to resolve the problem.
Thank you for fast response.
No problem - glad you're sorted. Looks like I should clarify the docs - thanks!