Skip to content

custom ostream formatter vs namespace vs clang #1550

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

Closed
anordal opened this issue Feb 12, 2020 · 2 comments
Closed

custom ostream formatter vs namespace vs clang #1550

anordal opened this issue Feb 12, 2020 · 2 comments

Comments

@anordal
Copy link

anordal commented Feb 12, 2020

I have basically #952 (comment) with a namespace:

#include <fmt/ostream.h>
#include <iostream>

namespace something{ struct Custom {}; }

std::ostream& operator<<(std::ostream& os, const something::Custom&)
{
	return os << "Ok";
}

int main()
{
	// Compiles with Gcc and Clang
	std::cout << something::Custom() << '\n';

	// Compiles with Gcc, but not Clang
	fmt::print("{}\n", something::Custom());
}

I don't know if this is a compiler bug, but it seems that clang + libfmt = operator<< not found.

The absurd thing is that it compiles (with both compilers, also the non-libfmt example) if I move operator<< into the namespace. That is not supposed to compile (if my understanding of argument dependent lookup is correct) – I learned this from the exact opposite experience.

Libfmt tested:

  • libfmt 6.1.2-1.2 (OpenSuse package)
  • latest git (dc22360)

Compilers tested:

  • g++ 9.2.1
  • g++ 7.4.1
  • clang++ 9.0.0
  • clang++ 6.0.1
@vitaut
Copy link
Contributor

vitaut commented Feb 12, 2020

I think clang is correct here because operator<< should be defined in the something namespace for ADL to work.

@vitaut vitaut closed this as completed Feb 12, 2020
@anordal
Copy link
Author

anordal commented Feb 12, 2020

I agree, after finding back my "exact opposite experience". That was with a type alias in the namespace. I forgot that distinction.

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