Skip to content
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

Can't format move-only ranges #3286

Closed
brevzin opened this issue Jan 25, 2023 · 0 comments · Fixed by #3290
Closed

Can't format move-only ranges #3286

brevzin opened this issue Jan 25, 2023 · 0 comments · Fixed by #3290

Comments

@brevzin
Copy link
Contributor

brevzin commented Jan 25, 2023

Example:

#include <concepts>
#include <vector>
#include <fmt/ranges.h>

template <bool Copyable>
struct Vector {
    std::vector<int> v;

    Vector(std::initializer_list<int> elems) : v(elems) { }

    Vector(Vector&&) = default;
    Vector& operator=(Vector&&) = default;

    Vector(Vector const&) requires Copyable = default;
    Vector& operator=(Vector const&) requires Copyable = default;

    auto begin() { return v.begin(); }
    auto end() { return v.end(); }
};

static_assert(std::movable<Vector<false>>);
static_assert(std::movable<Vector<true>>);
static_assert(!std::copyable<Vector<false>>);
static_assert(std::copyable<Vector<true>>);

int main() {
    fmt::print("{}\n", Vector<true>{1, 2, 3});  // ok [1, 2, 3]
    fmt::print("{}\n", Vector<false>{1, 2, 3}); // error
}

This is because the range check for non-const ranges requires copyability and should probably just be removed (h/t @timsong-cpp):

fmt/include/fmt/ranges.h

Lines 154 to 159 in a2c05a1

template <typename T>
struct has_mutable_begin_end<
T, void_t<decltype(detail::range_begin(std::declval<T>())),
decltype(detail::range_end(std::declval<T>())),
enable_if_t<std::is_copy_constructible<T>::value>>>
: std::true_type {};

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

Successfully merging a pull request may close this issue.

1 participant