C++17 mmmm :)

    if (auto [success, markerPos] = s->getClosestMarkerTo(pos, direction); success)
    {
        auto markerDist = std::abs(markerPos - pos);
        // do some stuff
    }

Well isn’t that a sexy new feature :slight_smile:

In case you’ve not tried it yet :slight_smile:

5 Likes

Structured bindings are particularly nice with enumerating iterators:

#include <array>
#include <fmt/format.h>
#include <range/v3/view/enumerate.hpp>

auto main() -> int {
  const auto collection = std::array{"foo", "bar", "baz"};

  for (const auto &[pos, value] : collection | ranges::views::enumerate) {
    fmt::print("value {} is at position {}\n", value, pos);
  }
}

when I view that link, i see this:

ASM generation compiler returned: 0

/tmp/example-0f1b29.o: In function `void fmt::v6::print<char [28], char const* const&, unsigned long const&, 0>(char const (&) [28], char const* const&, unsigned long const&)':

/opt/compiler-explorer/libs/fmt/6.0.0/include/fmt/core.h:1409: undefined reference to `fmt::v6::vprint(fmt::v6::basic_string_view<char>, fmt::v6::format_args)'

/opt/compiler-explorer/libs/fmt/6.0.0/include/fmt/core.h:1409: undefined reference to `fmt::v6::vprint(fmt::v6::basic_string_view<char>, fmt::v6::format_args)'

/opt/compiler-explorer/libs/fmt/6.0.0/include/fmt/core.h:1409: undefined reference to `fmt::v6::vprint(fmt::v6::basic_string_view<char>, fmt::v6::format_args)'

clang-9: error: linker command failed with exit code 1 (use -v to see invocation)

Execution build compiler returned: 1

That’s weird, it looks alright to me. Maybe the selected libraries aren’t saved as part of the shareable link. It should work if you select the latest ‘fmt’ and ‘ranges-v3’ from the libraries drop-down.

Oh I see. This is sort of a way of doing for (int i = 0…) only in a more hand-wavy modern programming way ? :slight_smile: What/where is this from : #include <range/v3/view/enumerate.hpp>

Yeah, it means you don’t have to declare a loop counter floating outside your loop body.

The #include comes from an installation of range-v3, which you could get via vcpkg or hunter and then link into your executable by doing (assuming you have a cmake build)

find_package(range-v3 CONFIG REQUIRED)
target_link_libraries(myexecutable PRIVATE range-v3)

Is that some horrible bastardisation of operator| ? :slight_smile:

It’s operator | yes, but I’m not going to make any moral judgements on it. This syntax is also in C++20:

#include <vector>
#include <ranges>
#include <iostream>
 
int main()
{
    std::vector ints{0,1,2,3,4,5};
    auto even = [](int i){ return 0 == i % 2; };
    auto square = [](int i) { return i * i; };
 
    for (auto i : ints | std::views::filter(even) | std::views::transform(square)) {
        std::cout << i << ' ';
    }
}

If it’s turning up in the standard I guess I’m going to have to get used to it :slight_smile:

Very happy to pass moral judgements though. Also i don’t like change :slight_smile:

1 Like