Почему мое использование std::uniform_random_bit_generator не работает?C++

Программы на C++. Форум разработчиков
Ответить
Anonymous
 Почему мое использование std::uniform_random_bit_generator не работает?

Сообщение Anonymous »

Приведен следующий пример программы, в которой используется концепция std::uniform_random_bit_generator:
#include
#include

template
void test(PRNG && rng) {
std::uniform_int_distribution dist{1, 7};
std::print("{}\n", dist(rng));
}

int main() {
std::minstd_rand engine{std::random_device{}()};
test(engine);
}

Я ожидаю, что этот код скомпилируется, но он не работает со следующими сообщениями об ошибках.
Меня смущает то, что я не определил никаких новых типы: здесь используется встроенный генератор случайных битов C++, и я получаю ту же ошибку при использовании других генераторов (mt19937 и т. д.). Почему это происходит? Я неправильно применяю эту концепцию?
Clang:
:12:5: error: no matching function for call to 'test'
12 | test(engine);
| ^~~~
:5:6: note: candidate template ignored: constraints not satisfied [with PRNG = std::minstd_rand &]
5 | void test(PRNG && rng) {
| ^
:4:10: note: because 'std::linear_congruential_engine &' does not satisfy 'uniform_random_bit_generator'
4 | template
| ^
/opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/15.0.0/../../../../include/c++/15.0.0/bits/uniform_int_dist.h:57:4: note: because '_Gen::min()' would be invalid: type 'std::linear_congruential_engine &' cannot be used prior to '::' because it has no members
57 | { _Gen::min() } -> same_as;
| ^
1 error generated.
Compiler returned: 1

GCC 14.2:
: In function 'int main()':
:12:9: error: no matching function for call to 'test(std::minstd_rand&)'
12 | test(engine);
| ~~~~^~~~~~~~
:5:6: note: candidate: 'template requires uniform_random_bit_generator void test(PRNG&&)'
5 | void test(PRNG && rng) {
| ^~~~
:5:6: note: template argument deduction/substitution failed:
:5:6: note: constraints not satisfied
In file included from /opt/compiler-explorer/gcc-14.2.0/include/c++/14.2.0/bits/random.h:35,
from /opt/compiler-explorer/gcc-14.2.0/include/c++/14.2.0/random:48,
from :1:
/opt/compiler-explorer/gcc-14.2.0/include/c++/14.2.0/bits/uniform_int_dist.h: In substitution of 'template requires uniform_random_bit_generator void test(PRNG&&) [with PRNG = std::linear_congruential_engine&]':
:12:9: required from here
12 | test(engine);
| ~~~~^~~~~~~~
/opt/compiler-explorer/gcc-14.2.0/include/c++/14.2.0/bits/uniform_int_dist.h:53:13: required for the satisfaction of 'uniform_random_bit_generator' [with PRNG = std::linear_congruential_engine&]
/opt/compiler-explorer/gcc-14.2.0/include/c++/14.2.0/bits/uniform_int_dist.h:55:10: in requirements [with _Gen = std::linear_congruential_engine&]
/opt/compiler-explorer/gcc-14.2.0/include/c++/14.2.0/bits/uniform_int_dist.h:57:20: note: the required expression '_Gen::min()' is invalid
57 | { _Gen::min() } -> same_as;
| ~~~~~~~~~^~
/opt/compiler-explorer/gcc-14.2.0/include/c++/14.2.0/bits/uniform_int_dist.h:58:20: note: the required expression '_Gen::max()' is invalid
58 | { _Gen::max() } -> same_as;
| ~~~~~~~~~^~
/opt/compiler-explorer/gcc-14.2.0/include/c++/14.2.0/bits/uniform_int_dist.h:59:18: note: nested requirement 'std::bool_constant::value' is not satisfied
59 | requires bool_constant::value;
| ~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
cc1plus: note: set '-fconcepts-diagnostics-depth=' to at least 2 for more detail
Compiler returned: 1


Подробнее здесь: https://stackoverflow.com/questions/793 ... or-failing
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «C++»