Skip to content

Commit cf822e4

Browse files
committed
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.
1 parent 1316b6d commit cf822e4

1 file changed

Lines changed: 12 additions & 1 deletion

File tree

include/rapidcheck/Gen.hpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22

3+
#include <type_traits>
34
#include <cassert>
45

56
#include "rapidcheck/detail/Any.h"
@@ -17,6 +18,13 @@ Gen<Decay<typename std::result_of<Mapper(T)>::type>> map(Gen<T> gen,
1718

1819
} // namespace gen
1920

21+
// Concept check for types that have method
22+
// `Shrinkable<T> G::operator()(const Random &, int) const`
23+
template<typename T, typename G>
24+
using MakesShrinkable = std::is_convertible<
25+
decltype(std::declval<const G>()(std::declval<Random>(), 0)),
26+
Shrinkable<T>>;
27+
2028
template <typename T>
2129
class Gen<T>::IGenImpl {
2230
public:
@@ -55,7 +63,10 @@ class Gen<T>::GenImpl : public IGenImpl {
5563
template <typename T>
5664
template <typename Impl, typename>
5765
Gen<T>::Gen(Impl &&impl)
58-
: m_impl(new GenImpl<Decay<Impl>>(std::forward<Impl>(impl))) {}
66+
: m_impl(new GenImpl<Decay<Impl>>(std::forward<Impl>(impl))) {
67+
static_assert(MakesShrinkable<T, Decay<Impl>>::value,
68+
"Generator implementation must have a method Shrinkable<T> operator()(const Random &, int) const");
69+
}
5970

6071
template <typename T>
6172
std::string Gen<T>::name() const {

0 commit comments

Comments
 (0)