Ниже приведен пример класса, который удовлетворяет этой теоретической концепции.
Код: Выделить всё
class RegularDie {
int _min, _max;
public:
constexpr RegularDie(int max, int min = 1): _max(max), _min(min) {
if(_max < _min) {
std::swap(_max, _min);
}
}
constexpr int min() const {return _min;}
constexpr int max() const {return _max;}
constexpr double oddsOf(int val) const {
if(val > _max || val < _min) {
return 0;
}
return 1. / (_max - _min + 1);
}
//This is the part I want the concept to test for
template
requires std::uniform_random_bit_generator
constexpr int operator()(PRNG && rng) const {
std::uniform_int_distribution dist{_min, _max};
return dist(rng);
}
};
int main() {
std::mt19937 engine{std::random_device{}()};
RegularDie cube{6};
std::print("First roll: {}\n", cube(engine));
std::minstd_rand engine2{std::random_device{}()};
std::print("Second roll: {}\n", cube(engine2));
}
Код: Выделить всё
template
concept Rollable = requires(Type t, PRNG prng) {
std::uniform_random_bit_generator;
{ t(prng) } -> std::convertible_to;
};
Код: Выделить всё
//Compiles, but doesn't prove the point, because the class might not accept other PRNGs
static_assert(Rollable);
Код: Выделить всё
template
concept Rollable = requires(Type t, std::mt19937 prng, std::minstd_rand prng2) {
std::uniform_random_bit_generator;
{ t(prng) } -> std::convertible_to;
{ t(prng2) } -> std::convertible_to;
};
//Compiles, but same problem. I've only tested two possible PRNGs for validity.
static_assert(Rollable);
- Можно ли вообще написать концепцию, которая будет ограничивать так, как я хочу?
- Если это возможно, как бы я это сделал?
Подробнее здесь: https://stackoverflow.com/questions/793 ... -its-argum