Catch2 icon indicating copy to clipboard operation
Catch2 copied to clipboard

Costness of MatcherMethod<PtrT*>

Open KarelCoudijzer opened this issue 9 years ago • 5 comments

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

KarelCoudijzer avatar Jul 20 '17 14:07 KarelCoudijzer

How are you deriving from MatcherBase?

philsquared avatar Jul 20 '17 14:07 philsquared

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.

KarelCoudijzer avatar Jul 20 '17 14:07 KarelCoudijzer

Try deriving from Catch::MatcherBase<T const*> and change match to: bool match(T const* other) const override

philsquared avatar Jul 20 '17 14:07 philsquared

This seems to resolve the problem.

Thank you for fast response.

KarelCoudijzer avatar Jul 20 '17 14:07 KarelCoudijzer

No problem - glad you're sorted. Looks like I should clarify the docs - thanks!

philsquared avatar Jul 20 '17 14:07 philsquared