Skip to content

Follow up to episode on better main #255

Open

Description

@lefticus

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;
    }
}

Activity

  1. JohelEGP commented on Mar 8, 2023

    @JohelEGP

    Previous episode's issue: #209.

  2. localspook commented on Aug 26, 2024

    @localspook

    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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions