diff --git a/docs/deprecations.md b/docs/deprecations.md index 643c73ce..4101f739 100644 --- a/docs/deprecations.md +++ b/docs/deprecations.md @@ -33,16 +33,6 @@ The API for Catch2's console colour will be changed to take an extra argument, the stream to which the colour code should be applied. -### Type erasure in the `PredicateMatcher` - -Currently, the `PredicateMatcher` uses `std::function` for type erasure, -so that type of the matcher is always `PredicateMatcher`, regardless -of the type of the predicate. Because of the high compilation overhead -of `std::function`, and the fact that the type erasure is used only rarely, -`PredicateMatcher` will no longer be type erased in the future. Instead, -the predicate type will be made part of the PredicateMatcher's type. - - --- [Home](Readme.md#top) diff --git a/docs/release-notes.md b/docs/release-notes.md index 4d0527a0..c9bc2f70 100644 --- a/docs/release-notes.md +++ b/docs/release-notes.md @@ -44,7 +44,8 @@ * If the second argument has text outside tags, the text will be ignored. * Hidden test cases are no longer included just because they don't match an exclusion tag * Previously, a `TEST_CASE("A", "[.foo]")` would be included by asking for `~[bar]`. - +* `PredicateMatcher` is no longer type erased. + * This means that the type of the provided predicate is part of the `PredicateMatcher`'s type ### Fixes diff --git a/include/internal/catch_generators.hpp b/include/internal/catch_generators.hpp index ea575c85..fd491696 100644 --- a/include/internal/catch_generators.hpp +++ b/include/internal/catch_generators.hpp @@ -14,7 +14,7 @@ #include #include #include - +#include #include #include diff --git a/include/internal/catch_matchers_generic.hpp b/include/internal/catch_matchers_generic.hpp index 7d57c9d4..eaef5485 100644 --- a/include/internal/catch_matchers_generic.hpp +++ b/include/internal/catch_matchers_generic.hpp @@ -9,9 +9,10 @@ #include "catch_common.h" #include "catch_matchers.h" +#include "catch_meta.hpp" -#include #include +#include namespace Catch { namespace Matchers { @@ -21,14 +22,14 @@ namespace Detail { std::string finalizeDescription(const std::string& desc); } -template +template class PredicateMatcher : public MatcherBase { - std::function m_predicate; + Predicate m_predicate; std::string m_description; public: - PredicateMatcher(std::function const& elem, std::string const& descr) - :m_predicate(std::move(elem)), + PredicateMatcher(Predicate&& elem, std::string const& descr) + :m_predicate(std::forward(elem)), m_description(Detail::finalizeDescription(descr)) {} @@ -47,9 +48,11 @@ public: // The user has to explicitly specify type to the function, because // inferring std::function is hard (but possible) and // requires a lot of TMP. - template - Generic::PredicateMatcher Predicate(std::function const& predicate, std::string const& description = "") { - return Generic::PredicateMatcher(predicate, description); + template + Generic::PredicateMatcher Predicate(Pred&& predicate, std::string const& description = "") { + static_assert(is_callable::value, "Predicate not callable with argument T"); + static_assert(std::is_same>::value, "Predicate does not return bool"); + return Generic::PredicateMatcher(std::forward(predicate), description); } } // namespace Matchers