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

ostream support is broken #3318

Closed
adah1972 opened this issue Feb 25, 2023 · 3 comments
Closed

ostream support is broken #3318

adah1972 opened this issue Feb 25, 2023 · 3 comments
Labels

Comments

@adah1972
Copy link

Given this test code:

#include <vector>
#include <fmt/core.h>
#include <fmt/ostream.h>

using namespace std;

std::ostream& operator<<(std::ostream& os, const std::vector<int>& v)
{
    for (int n : v) {
        os << n << ' ';
    }
    return os;
}

int main()
{
    vector<int> v{1, 2, 3};
    fmt::print(stdout, "{}\n", v);
}

It works with GCC 9/10/11 (both C++17 and C++20/C++2a modes) and fmt v8.1.1 (as well as many earlier versions).

From fmt v9.0.0 (up to head in master), the code above always fail.

Also, even with fmt v8.1.1, I am not able to make it work with GCC 12 or Clang.

@vitaut
Copy link
Contributor

vitaut commented Feb 25, 2023

There were multiple problems with automatic ostream insertion operator discovery and it was disabled in v9. Please see the docs on how to provide an explicit ostream-based formatter: https://fmt.dev/latest/api.html#ostream-api. Note that you shouldn't normally overload operator<< or provide a formatter for types you don't own such as std::vector<int>. For vector I'd recommend using ranges formatting instead: https://fmt.dev/latest/api.html#ranges-api.

@adah1972
Copy link
Author

vector is just an example: I actually used to use a range stream outputter, which worked with older versions of fmt for years.

Still glad to know that there is a solution (and the fmt/ranges.h option, which I had missed so far). Thanks.

@harryjwright
Copy link

This is the biggest issue with this library I have found. When everyone is so used overloading the << operator, why not focus on support for that out of the box? It doesn't make any sense.

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

No branches or pull requests

3 participants