Skip to content

Commit

Permalink
Give a more helpful error message when Gen is constructed with incomp…
Browse files Browse the repository at this point in the history
…atible type

This produces a compile error in Gen's constructor.  Before this change,
errors would be attributed to GenImpl.
  • Loading branch information
cirodrig committed Sep 19, 2017
1 parent 1316b6d commit cf822e4
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion include/rapidcheck/Gen.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <type_traits>
#include <cassert>

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

} // namespace gen

// Concept check for types that have method
// `Shrinkable<T> G::operator()(const Random &, int) const`
template<typename T, typename G>
using MakesShrinkable = std::is_convertible<
decltype(std::declval<const G>()(std::declval<Random>(), 0)),
Shrinkable<T>>;

template <typename T>
class Gen<T>::IGenImpl {
public:
Expand Down Expand Up @@ -55,7 +63,10 @@ class Gen<T>::GenImpl : public IGenImpl {
template <typename T>
template <typename Impl, typename>
Gen<T>::Gen(Impl &&impl)
: m_impl(new GenImpl<Decay<Impl>>(std::forward<Impl>(impl))) {}
: m_impl(new GenImpl<Decay<Impl>>(std::forward<Impl>(impl))) {
static_assert(MakesShrinkable<T, Decay<Impl>>::value,
"Generator implementation must have a method Shrinkable<T> operator()(const Random &, int) const");
}

template <typename T>
std::string Gen<T>::name() const {
Expand Down

0 comments on commit cf822e4

Please sign in to comment.