Skip to content

Instantly share code, notes, and snippets.

@kfish
Created August 11, 2018 04:49
Show Gist options
  • Save kfish/90002e1eb88546e17a98b70cfd109c28 to your computer and use it in GitHub Desktop.
Save kfish/90002e1eb88546e17a98b70cfd109c28 to your computer and use it in GitHub Desktop.
Rapidcheck(C++) Arbitrary instance for `std::variant`
// An Arbitrary instance generator for std::variant, for
// Rapidcheck: https://github.com/emil-e/rapidcheck
#include <variant>
namespace rc {
template<>
struct Arbitrary<std::monostate> {
static Gen<std::monostate> arbitrary() {
return gen::element(std::monostate());
}
};
template <typename T, typename... Ts>
struct Arbitrary<std::variant<T, Ts...>> {
static Gen<std::variant<T, Ts...>> arbitrary() {
return gen::oneOf(
gen::cast<std::variant<T, Ts...>>(gen::arbitrary<T>()),
gen::cast<std::variant<T, Ts...>>(gen::arbitrary<Ts>())...);
}
};
} // namespace rc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment