Как итерация по проездам во время компиляции?#include
#include
#include
namespace {
class Solution {
public:
template [[nodiscard]] consteval int maxArea(
std::array const& /*height*/) const {
return 1;
}
};
template struct TestStruct {
std::array numbers;
int expected_result;
};
} // namespace
int main() {
constexpr auto data_test = std::make_tuple(
TestStruct{.numbers = std::to_array({1, 1}), .expected_result = 1},
TestStruct{.numbers = std::to_array({1, 2, 3}), .expected_result = 2});
// for element in tuple:
{
constexpr Solution functor;
static_assert(functor.maxArea(element.numbers)
== element.expected_result);
}
}
вручную итерацию без tuple (работает) https://godbolt.org/z/tmcbettpv:
constexpr TestStruct test_data1{.numbers = std::to_array({1, 1}),
.expected_result = 1};
constexpr TestStruct test_data2{.numbers = std::to_array({1, 2, 3}),
.expected_result = 2};
constexpr Solution functor;
static_assert(functor.maxArea(test_data1.numbers)
== test_data1.expected_result);
static_assert(functor.maxArea(test_data2.numbers)
== test_data2.expected_result);
с итерацией вручную (работает) https://godbolt.org/z/b3wrqo3gy:
int main() {
constexpr auto data_test = std::make_tuple(
TestStruct{.numbers = std::to_array({1, 1}), .expected_result = 1},
TestStruct{.numbers = std::to_array({1, 2, 3}), .expected_result = 2});
constexpr Solution functor;
static_assert(functor.maxArea(std::get(data_test).numbers)
== std::get(data_test).expected_result);
static_assert(functor.maxArea(std::get(data_test).numbers)
== std::get(data_test).expected_result);
}
с помощью Tuple iterate через std :: Apply (не работает) https://godbolt.org/z/vaa673b6a:
написан std::apply(
[](auto const& test_struct) {
constexpr Solution functor;
static_assert(functor.maxArea(test_struct.numbers)
== test_struct.expected_result);
},
data_test);
< /code>
boost :: mp11 tuple_for_each (f, tp) (не работает): < /p>
boost::mp11::tuple_for_each(data_test, [](auto const& test_struct) {
constexpr solution functor;
static_assert(functor.maxarea(test_struct.numbers)
== test_struct.expected_result);
});
вручную записывает на for_each через std :: index_sectence: (не работает) https://godbolt.org/z/5j46eqjwr
образно template
consteval void test_with_TestStruct(TestStructconst & test_data) {
constexpr Solution functor;
static_assert(functor.maxArea(test_data.numbers)
== test_data.expected_result);
}
template
consteval void test_impl(Tuple tuple_to_apply_at,
std::index_sequence /*unused*/) {
(test_with_TestStruct(std::get(tuple_to_apply_at)), ...);
}
template consteval auto test(Tuple tuple_to_apply_at) {
constexpr auto size = std::tuple_size::value;
test_impl(tuple_to_apply_at, std::make_index_sequence{});
}
int main() {
constexpr auto data_test = std::make_tuple(
TestStruct{.numbers = std::to_array({1, 1}), .expected_result = 1},
TestStruct{.numbers = std::to_array({1, 2, 3}), .expected_result = 2});
test(data_test);
}
Подробнее здесь: https://stackoverflow.com/questions/797 ... mpile-time
C ++: Как итерация через кортеж во время компиляции? ⇐ C++
Программы на C++. Форум разработчиков
-
Anonymous
1753031541
Anonymous
Как итерация по проездам во время компиляции?#include
#include
#include
namespace {
class Solution {
public:
template [[nodiscard]] consteval int maxArea(
std::array const& /*height*/) const {
return 1;
}
};
template struct TestStruct {
std::array numbers;
int expected_result;
};
} // namespace
int main() {
constexpr auto data_test = std::make_tuple(
TestStruct{.numbers = std::to_array({1, 1}), .expected_result = 1},
TestStruct{.numbers = std::to_array({1, 2, 3}), .expected_result = 2});
// for element in tuple:
{
constexpr Solution functor;
static_assert(functor.maxArea(element.numbers)
== element.expected_result);
}
}
вручную итерацию без tuple (работает) https://godbolt.org/z/tmcbettpv:
constexpr TestStruct test_data1{.numbers = std::to_array({1, 1}),
.expected_result = 1};
constexpr TestStruct test_data2{.numbers = std::to_array({1, 2, 3}),
.expected_result = 2};
constexpr Solution functor;
static_assert(functor.maxArea(test_data1.numbers)
== test_data1.expected_result);
static_assert(functor.maxArea(test_data2.numbers)
== test_data2.expected_result);
с итерацией вручную (работает) https://godbolt.org/z/b3wrqo3gy:
int main() {
constexpr auto data_test = std::make_tuple(
TestStruct{.numbers = std::to_array({1, 1}), .expected_result = 1},
TestStruct{.numbers = std::to_array({1, 2, 3}), .expected_result = 2});
constexpr Solution functor;
static_assert(functor.maxArea(std::get(data_test).numbers)
== std::get(data_test).expected_result);
static_assert(functor.maxArea(std::get(data_test).numbers)
== std::get(data_test).expected_result);
}
с помощью Tuple iterate через std :: Apply (не работает) https://godbolt.org/z/vaa673b6a:
написан std::apply(
[](auto const& test_struct) {
constexpr Solution functor;
static_assert(functor.maxArea(test_struct.numbers)
== test_struct.expected_result);
},
data_test);
< /code>
boost :: mp11 tuple_for_each (f, tp) (не работает): < /p>
boost::mp11::tuple_for_each(data_test, [](auto const& test_struct) {
constexpr solution functor;
static_assert(functor.maxarea(test_struct.numbers)
== test_struct.expected_result);
});
вручную записывает на for_each через std :: index_sectence: (не работает) https://godbolt.org/z/5j46eqjwr
образно template
consteval void test_with_TestStruct(TestStructconst & test_data) {
constexpr Solution functor;
static_assert(functor.maxArea(test_data.numbers)
== test_data.expected_result);
}
template
consteval void test_impl(Tuple tuple_to_apply_at,
std::index_sequence /*unused*/) {
(test_with_TestStruct(std::get(tuple_to_apply_at)), ...);
}
template consteval auto test(Tuple tuple_to_apply_at) {
constexpr auto size = std::tuple_size::value;
test_impl(tuple_to_apply_at, std::make_index_sequence{});
}
int main() {
constexpr auto data_test = std::make_tuple(
TestStruct{.numbers = std::to_array({1, 1}), .expected_result = 1},
TestStruct{.numbers = std::to_array({1, 2, 3}), .expected_result = 2});
test(data_test);
}
Подробнее здесь: [url]https://stackoverflow.com/questions/79708135/c-how-to-iterate-over-tuple-in-compile-time[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия