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 #247

Closed
bumbar1 opened this issue Dec 18, 2015 · 3 comments
Closed

operator bool #247

bumbar1 opened this issue Dec 18, 2015 · 3 comments

Comments

@bumbar1
Copy link

bumbar1 commented Dec 18, 2015

Not sure if it's a bug (maybe it's expected behavior because of bool?), but if you define a custom data type with operator bool (regardless of return value) and operator <<, then formatting doesn't work.

Example code:

#include <iostream>
#include <string>
#include "cppformat/format.h"

struct Date {
    operator bool() const { return true; }
    friend
    std::ostream& operator << (std::ostream& os, const Date& date) { return os << "1.1.1970"; }
};

int main() {
    std::cout << "date is " << Date{} << "\n";
    std::cout << fmt::format("date is {}\n", Date{}); // prints "date is 1"
}
@vitaut
Copy link
Contributor

vitaut commented Dec 18, 2015

I think this has been fixed already in 6cff6d8. The commit message mentions enums but the same applies for any type implicitly convertible to int. Using the current master branch I get:

date is 1.1.1970
date is 1.1.1970

@bumbar1
Copy link
Author

bumbar1 commented Dec 18, 2015

Ah yes, apologies. I wasn't using the latest version. It's working with operator bool defined. Thanks.

@bumbar1 bumbar1 closed this as completed Dec 18, 2015
@vitaut
Copy link
Contributor

vitaut commented Dec 18, 2015

No problems. Thanks for the confirmation.

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