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

Unexpected formatting of bool-convertible custom type #1503

Closed
benoitryder opened this issue Dec 31, 2019 · 1 comment
Closed

Unexpected formatting of bool-convertible custom type #1503

benoitryder opened this issue Dec 31, 2019 · 1 comment

Comments

@benoitryder
Copy link

If a custom type defines both operator bool and operator<<, it is formatted as bool instead of using the custom operator<<.
According to git bisect, behavior changed with ccc8f5d to fix #1316.

This seems to be the same issue as #247. (I don't know if the expected behavior changed since then.)

Example code (Godbolt):

#define FMT_HEADER_ONLY
#include <fmt/ostream.h>

struct S {
    operator bool() const { return true; }
};

std::ostream& operator<<(std::ostream& os, S const&) { return os << "s-value"; }

int main() {
    fmt::print("s: {}\n", S{});
}

Prints s: true instead of s: s-value.

@vitaut
Copy link
Contributor

vitaut commented Jan 3, 2020

I don't see a way to make this work without breaking a much more important fix of #1316. Since having an implicit conversion to bool is a bad idea in general, I recommend making it explicit. Alternatively you can provide a custom formatter<S>: https://fmt.dev/latest/api.html#formatting-user-defined-types

@vitaut vitaut closed this as completed Jan 3, 2020
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

No branches or pull requests

2 participants