As has been answered before, namespacing operator overloads is considered good practice, so that's what I want to do.
Problem: It only compiles if I don't. Am I just doing it wrong, or have I found an exception where it is not possible?
Here is a single translation unit for easy repro:
// lib/halfseconds.h:
#include <chrono>
namespace lib {
using halfseconds = std::chrono::duration<intmax_t, std::ratio<1, 2> >;
}
// lib/debug.h:
#include <ostream>
namespace lib {
std::ostream& operator<<(std::ostream& o, lib::halfseconds halves)
{
double seconds = halves.count();
seconds /= lib::halfseconds::period::den;
o << seconds << 's';
return o;
}
}
// demo/main.cpp:
#include <iostream>
int main()
{
lib::halfseconds threeHalvseconds(3);
std::cout << threeHalvseconds << '\n'; // 1.5s
}
What does the compiler say? G++ version 8.2.1 says «no match for operator<<» and spews one daunting list (208 lines) of candidates. I suppose none of those are relevant, as I wouldn't get this error if the relevant one wasn't missing.