Telegram Group & Telegram Channel
Что выведет этот код? (C++23)


import std;

constexpr auto make_checker() {
return [](int x) consteval {
return x % 3 == 0 || x % 5 == 0;
};
}

int main() {
auto checker = make_checker();

std::vector numbers{1, 3, 5, 9, 10, 14, 15};

auto filtered = numbers | std::views::filter([&](int x) {
if (std::is_constant_evaluated()) {
std::print("constexpr\n");
}
return checker(x);
});

std::print("Filtered numbers: ");
for (int x : filtered) {
std::print("{} ", x);
}

std::println("");
}


🧠 Подсказка:
consteval делает checker доступным только в compile-time, но мы вызываем его в runtime через лямбду — что произойдёт?

std::is_constant_evaluated() — интересный механизм проверки, вызывается ли код во время компиляции.

Как отреагирует компилятор на попытку вызвать consteval функцию в runtime?


📌 Ответ
Этот код на C++23 не скомпилируется, и что именно здесь происходит.

🔍 Напоминаем ключевой фрагмент кода:

```cpp
constexpr auto make_checker() {
return [](int x) consteval {
return x % 3 == 0 || x % 5 == 0;
};
} ```

- Здесь создаётся лямбда-функция, помеченная как consteval.

- Ключевое слово consteval означает: функция обязана быть вызвана во время компиляции.

🧨 Где ошибка?


auto filtered = numbers | std::views::filter([&](int x) {
return checker(x); // ← ошибка тут
});


checker — это consteval-лямбда.

Но ты вызываешь её внутри лямбды, которая будет работать во время выполнения программы — т.е. в runtime.

Это нарушение правила consteval → нельзя вызывать такие функции в runtime-коде.

Что скажет компилятор?
Компилятор выдаст ошибку компиляции, такую или похожую:

error: call to consteval function '<lambda>(int)' is not a constant expression



📘 Объяснение
consteval ≠ constexpr

constexpr — это могут быть вызваны в runtime, если нужно.

consteval — это всегда и только compile-time.

Когда ты вызываешь checker(x) в main(), ты нарушаешь это правило.

Как можно исправить?
Если ты заменишь consteval на constexpr, код скомпилируется и выполнится:


constexpr auto make_checker() {
return [](int x) constexpr {
return x % 3 == 0 || x % 5 == 0;
};
}

И тогда результат будет:

Filtered numbers: 3 5 9 10 15

Потому что:
- 3 делится на 3
- 5 делится на 5
- 9 делится на 3
- 10 делится на 5
- 15 делится на 3 и 5



tg-me.com/cpluspluc/1026
Create:
Last Update:

Что выведет этот код? (C++23)


import std;

constexpr auto make_checker() {
return [](int x) consteval {
return x % 3 == 0 || x % 5 == 0;
};
}

int main() {
auto checker = make_checker();

std::vector numbers{1, 3, 5, 9, 10, 14, 15};

auto filtered = numbers | std::views::filter([&](int x) {
if (std::is_constant_evaluated()) {
std::print("constexpr\n");
}
return checker(x);
});

std::print("Filtered numbers: ");
for (int x : filtered) {
std::print("{} ", x);
}

std::println("");
}


🧠 Подсказка:
consteval делает checker доступным только в compile-time, но мы вызываем его в runtime через лямбду — что произойдёт?

std::is_constant_evaluated() — интересный механизм проверки, вызывается ли код во время компиляции.

Как отреагирует компилятор на попытку вызвать consteval функцию в runtime?


📌 Ответ
Этот код на C++23 не скомпилируется, и что именно здесь происходит.

🔍 Напоминаем ключевой фрагмент кода:

```cpp
constexpr auto make_checker() {
return [](int x) consteval {
return x % 3 == 0 || x % 5 == 0;
};
} ```

- Здесь создаётся лямбда-функция, помеченная как consteval.

- Ключевое слово consteval означает: функция обязана быть вызвана во время компиляции.

🧨 Где ошибка?


auto filtered = numbers | std::views::filter([&](int x) {
return checker(x); // ← ошибка тут
});


checker — это consteval-лямбда.

Но ты вызываешь её внутри лямбды, которая будет работать во время выполнения программы — т.е. в runtime.

Это нарушение правила consteval → нельзя вызывать такие функции в runtime-коде.

Что скажет компилятор?
Компилятор выдаст ошибку компиляции, такую или похожую:

error: call to consteval function '<lambda>(int)' is not a constant expression



📘 Объяснение
consteval ≠ constexpr

constexpr — это могут быть вызваны в runtime, если нужно.

consteval — это всегда и только compile-time.

Когда ты вызываешь checker(x) в main(), ты нарушаешь это правило.

Как можно исправить?
Если ты заменишь consteval на constexpr, код скомпилируется и выполнится:


constexpr auto make_checker() {
return [](int x) constexpr {
return x % 3 == 0 || x % 5 == 0;
};
}

И тогда результат будет:

Filtered numbers: 3 5 9 10 15

Потому что:
- 3 делится на 3
- 5 делится на 5
- 9 делится на 3
- 10 делится на 5
- 15 делится на 3 и 5

BY C++ Academy


Warning: Undefined variable $i in /var/www/tg-me/post.php on line 283

Share with your friend now:
tg-me.com/cpluspluc/1026

View MORE
Open in Telegram


telegram Telegram | DID YOU KNOW?

Date: |

The seemingly negative pandemic effects and resource/product shortages are encouraging and allowing organizations to innovate and change.The news of cash-rich organizations getting ready for the post-Covid growth economy is a sign of more than capital spending plans. Cash provides a cushion for risk-taking and a tool for growth.

Telegram Auto-Delete Messages in Any Chat

Some messages aren’t supposed to last forever. There are some Telegram groups and conversations where it’s best if messages are automatically deleted in a day or a week. Here’s how to auto-delete messages in any Telegram chat. You can enable the auto-delete feature on a per-chat basis. It works for both one-on-one conversations and group chats. Previously, you needed to use the Secret Chat feature to automatically delete messages after a set time. At the time of writing, you can choose to automatically delete messages after a day or a week. Telegram starts the timer once they are sent, not after they are read. This won’t affect the messages that were sent before enabling the feature.

telegram from us


Telegram C++ Academy
FROM USA