C ++: Как итерация через кортеж во время компиляции?C++

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

Сообщение 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);
}



Подробнее здесь: https://stackoverflow.com/questions/797 ... mpile-time
Ответить

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

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

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

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

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