From cf822e4b9b726a1dc8f09ff934faf5eb6e5385b7 Mon Sep 17 00:00:00 2001 From: Christopher Rodrigues Date: Sat, 16 Sep 2017 11:34:54 -0700 Subject: [PATCH] Give a more helpful error message when Gen is constructed with incompatible type This produces a compile error in Gen's constructor. Before this change, errors would be attributed to GenImpl. --- include/rapidcheck/Gen.hpp | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/include/rapidcheck/Gen.hpp b/include/rapidcheck/Gen.hpp index 7d8f124e..65517e9e 100644 --- a/include/rapidcheck/Gen.hpp +++ b/include/rapidcheck/Gen.hpp @@ -1,5 +1,6 @@ #pragma once +#include #include #include "rapidcheck/detail/Any.h" @@ -17,6 +18,13 @@ Gen::type>> map(Gen gen, } // namespace gen +// Concept check for types that have method +// `Shrinkable G::operator()(const Random &, int) const` +template +using MakesShrinkable = std::is_convertible< + decltype(std::declval()(std::declval(), 0)), + Shrinkable>; + template class Gen::IGenImpl { public: @@ -55,7 +63,10 @@ class Gen::GenImpl : public IGenImpl { template template Gen::Gen(Impl &&impl) - : m_impl(new GenImpl>(std::forward(impl))) {} + : m_impl(new GenImpl>(std::forward(impl))) { + static_assert(MakesShrinkable>::value, + "Generator implementation must have a method Shrinkable operator()(const Random &, int) const"); +} template std::string Gen::name() const {