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

operator bool takes precedence over ostream after 6.1 #2624

Closed
timblechmann opened this issue Nov 29, 2021 · 3 comments
Closed

operator bool takes precedence over ostream after 6.1 #2624

timblechmann opened this issue Nov 29, 2021 · 3 comments

Comments

@timblechmann
Copy link
Contributor

class Symbol
{
public:
  string string() const { return m_string; }
  operator bool() const { return !m_string.empty(); }

private:
  std::string m_string {"test"};
};

ostream& operator<< (ostream& os, const Symbol& symbol)
{
    os << symbol.string();
    return os;
}

the implicit bool conversion confuses fmt:

  • 6.0: format / sprintf give "test"
  • 6.1: format gives "true, sprintf gives "test"
  • 6.2: format / sprintf give "true"

my coworker came up with this reproducer on godbolt: https://godbolt.org/z/ffGf49W43

@vitaut
Copy link
Contributor

vitaut commented Nov 29, 2021

Unfortunately there is no good way to handle ambiguous cases like this. You can provide an formatter specialization to suppress the implicit conversion.

@vitaut vitaut closed this as completed Nov 29, 2021
@timblechmann
Copy link
Contributor Author

unfortunately the custom formatter is only applied with fmt::format, not with fmt::sprintf

@vitaut
Copy link
Contributor

vitaut commented Nov 29, 2021

sprintf is mostly for migrating legacy code and doesn't support ostreams or other extension mechanisms.

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