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

Fix compilation error for ranges with ADL begin/end #2807

Merged
merged 4 commits into from
Mar 11, 2022

Conversation

rbrugo
Copy link
Contributor

@rbrugo rbrugo commented Mar 11, 2022

This PR should fix an issue that occurs when the range formatter is unable to use begin and end found by ADL.
For example, this code currently fails to compile:

namespace adl
{
struct box
{
    int value;
};

int * begin(box & b) noexcept { return std::addressof(b.value); }
int * end(box & b)   noexcept { return std::addressof(b.value) + 1; }

int const * begin(box const & b) noexcept { return std::addressof(b.value); }
int const * end(box const & b)   noexcept { return std::addressof(b.value) + 1; }
} // namespace adl

int main()
{
    auto x = adl::box{42};
    
    // this works
    for (auto elem : x) {
        fmt::print("{}\n", elem);
    }
    
    // this compiles correctly
    static_assert(fmt::is_range<adl::box, char>::value, "");

    // but this does not compile
    fmt::print("{}\n", x);
}

but with the proposed patch works as expected.

@phprus
Copy link
Contributor

phprus commented Mar 11, 2022

please apply clang-format

@rbrugo
Copy link
Contributor Author

rbrugo commented Mar 11, 2022

I completely forgot to run it on the tests. Thank you for pointing out.

Copy link
Contributor

@vitaut vitaut left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR.

test/ranges-test.cc Outdated Show resolved Hide resolved
test/ranges-test.cc Outdated Show resolved Hide resolved
@vitaut vitaut merged commit a8fe8be into fmtlib:master Mar 11, 2022
@vitaut
Copy link
Contributor

vitaut commented Mar 11, 2022

Thank you!

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 this pull request may close these issues.

3 participants