Почему это не работает (GCC с STD 26)?#include
#include
template
consteval auto identity()
{
std::array mat;
for (int i = 0; i < M; ++i)
for (int j = 0; j < N; ++j)
mat[i * N + j] = i == j;
return mat;
}
consteval auto count_non_zeros(std::ranges::input_range auto&& range)
{
size_t i = 0;
for (auto x : range)
i += x != 0;
return i;
}
template
consteval auto create_triplets(
std::ranges::input_range auto&& mat)
{
using Tp = std::tuple;
constexpr auto non_zeros = count_non_zeros(mat);
auto tplist = std::array {};
auto it = tplist.begin();
for (size_t i = 0; i < M; ++i)
for (size_t j = 0; j < N; ++j) {
const auto val = mat[i * N + j];
if (val == 0)
continue;
*it++ = Tp(i, j, val);
}
return tplist;
}
int main()
{
constexpr auto N = 10, M = 10;
constexpr auto mat = identity();
constexpr auto tplist = create_triplets(mat);
return 0;
}
< /code>
Сообщение об ошибке: < /p>
main.cc: In instantiation of ‘consteval auto create_triplets(auto:49&&) [with long unsigned int N = 10; long unsigned int M = 10; T = float; auto:49 = const std::array&]’:
main.cc:46:50: required from here
46 | constexpr auto tplist = create_triplets(mat);
| ~~~~~~~~~~~~~~~~~~~~~^~~~~
main.cc:27:47: in ‘constexpr’ expansion of ‘count_non_zeros((* & mat))’
main.cc:17:15: error: ‘* & mat’ is not a constant expression
17 | for (auto x : range)
| ^
Подробнее здесь: https://stackoverflow.com/questions/794 ... l-function
Ошибка: '* & mat' не является постоянным выражением в постоянной функции ⇐ C++
Программы на C++. Форум разработчиков
1739736144
Anonymous
Почему это не работает (GCC с STD 26)?#include
#include
template
consteval auto identity()
{
std::array mat;
for (int i = 0; i < M; ++i)
for (int j = 0; j < N; ++j)
mat[i * N + j] = i == j;
return mat;
}
consteval auto count_non_zeros(std::ranges::input_range auto&& range)
{
size_t i = 0;
for (auto x : range)
i += x != 0;
return i;
}
template
consteval auto create_triplets(
std::ranges::input_range auto&& mat)
{
using Tp = std::tuple;
constexpr auto non_zeros = count_non_zeros(mat);
auto tplist = std::array {};
auto it = tplist.begin();
for (size_t i = 0; i < M; ++i)
for (size_t j = 0; j < N; ++j) {
const auto val = mat[i * N + j];
if (val == 0)
continue;
*it++ = Tp(i, j, val);
}
return tplist;
}
int main()
{
constexpr auto N = 10, M = 10;
constexpr auto mat = identity();
constexpr auto tplist = create_triplets(mat);
return 0;
}
< /code>
Сообщение об ошибке: < /p>
main.cc: In instantiation of ‘consteval auto create_triplets(auto:49&&) [with long unsigned int N = 10; long unsigned int M = 10; T = float; auto:49 = const std::array&]’:
main.cc:46:50: required from here
46 | constexpr auto tplist = create_triplets(mat);
| ~~~~~~~~~~~~~~~~~~~~~^~~~~
main.cc:27:47: in ‘constexpr’ expansion of ‘count_non_zeros((* & mat))’
main.cc:17:15: error: ‘* & mat’ is not a constant expression
17 | for (auto x : range)
| ^
Подробнее здесь: [url]https://stackoverflow.com/questions/79443844/error-mat-is-not-a-constant-expression-in-a-consteval-function[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия