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

format user-defined type get a compile error after 8.0.1 #3509

Closed
subenle-nreal opened this issue Jun 26, 2023 · 3 comments
Closed

format user-defined type get a compile error after 8.0.1 #3509

subenle-nreal opened this issue Jun 26, 2023 · 3 comments

Comments

@subenle-nreal
Copy link

test code is listed below。It works well with version 8.0.1,but will get a compile error with newer version even the newest version 10.0.0。when build with 10.0.0,I uncomment two formmater in the code, just as the document list。

#include <fmt/ostream.h>
#include <iostream>
class date {
  int year_, month_, day_;
public:
  date(int year, int month, int day): year_(year), month_(month), day_(day) {}

  friend std::ostream& operator<<(std::ostream& os, const date& d) {
    return os << d.year_ << '-' << d.month_ << '-' << d.day_;
  }
};

class MyVector3f {
   public:
    float x = 1;
    float y = 2;
    float z = 3;

   public:
    MyVector3f() {}
    template <typename LocalType>
    MyVector3f(const LocalType& v) {
        this->x = v.x();
        this->y = v.y();
        this->z = v.z();
    }

    template <typename LocalType>
    operator LocalType() const {
        LocalType v;
        v.x() = this->x;
        v.y() = this->y;
        v.z() = this->z;
        return v;
    }

    template <typename OStream>
 	friend OStream& operator<<(OStream& os, const MyVector3f& v) {
    return os << "["
              << " x:" << v.x << ", y:" << v.y << ", z:" << v.z << "]";
}
};

//  When build with 10.0.0, please uncomment the two lines below.
// template <> struct fmt::formatter<date> : ostream_formatter {};
// template <> struct fmt::formatter<MyVector3f> : ostream_formatter{};


int main()
{
	std::string s = fmt::format("The date is {}", date(2012, 12, 9));
	std::cout << s << std::endl;

	std::string vs = fmt::format("vector3f is {}", MyVector3f());
	std::cout << vs << std::endl;
}
@vitaut
Copy link
Contributor

vitaut commented Jun 26, 2023

Repro: https://godbolt.org/z/WWod8GdhP.

The problem is that MyVector3f has an implicit conversion operator that tries to convert to anything but fails if it doesn't have members x(), etc. which interferes with built-in {fmt}'s formatters. I would recommend constraining it, e.g.

    template <typename LocalType, size_t = sizeof(LocalType().x())>
    operator LocalType() const {
        LocalType v;
        v.x() = this->x;
        v.y() = this->y;
        v.z() = this->z;
        return v;
    }

@subenle-nreal
Copy link
Author

Thanks, it really works.

@vitaut
Copy link
Contributor

vitaut commented Jun 28, 2023

Fixed in 606f85f.

@vitaut vitaut closed this as completed Jun 28, 2023
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