Skip to content

Follow up to episode on better main #255

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
lefticus opened this issue Mar 4, 2023 · 2 comments
Open

Follow up to episode on better main #255

lefticus opened this issue Mar 4, 2023 · 2 comments

Comments

@lefticus
Copy link
Owner

lefticus commented Mar 4, 2023

Here is a fully lazy way to do the span of string_views that doesn't require any heap.

#include <span>
#include <ranges>
#include <iostream>

int main(const int argc, const char *argv[])
{
    for (auto param : std::span(argv, argv + argc) | std::views::transform([](const auto *c_str){ return std::string_view(c_str); })) {
        std::cout << param;
    }
}
@JohelEGP
Copy link

JohelEGP commented Mar 8, 2023

Previous episode's issue: #209.

@LocalSpook
Copy link

LocalSpook commented Aug 26, 2024

A way of writing it that avoids the pointer arithmetic with std::views::counted:

#include <ranges>
#include <iostream>

int main(const int argc, const char *argv[])
{
    for (std::string_view param : std::views::counted(argv, argc)) {
        std::cout << param;
    }
}

(Check that codegen is identical on Compiler Explorer)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants