Приведен следующий пример программы, в которой используется концепция 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
Почему мое использование std::uniform_random_bit_generator не работает? ⇐ C++
Программы на C++. Форум разработчиков
1735423038
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
Подробнее здесь: [url]https://stackoverflow.com/questions/79314588/why-is-my-use-of-stduniform-random-bit-generator-failing[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия