Spongman
Spongman

Reputation: 9871

how to use date.h operator<< system_clock for class defined in namespace?

Why doesn't this compile?

#include <iostream>
#include <chrono>
#include "date.h"

namespace NS
{
    class C {};

    std::ostream &operator<<(std::ostream &o, const C &di)
    {
        using namespace date;
        using namespace std::chrono;
        std::cout << ::std::chrono::system_clock::now();
        return o;
    }
}
<source>:8250:19: error: invalid operands to binary expression ('std::ostream' (aka 'basic_ostream<char>') and 'std::chrono::system_clock::time_point' (aka 'time_point<std::chrono::system_clock, duration<long, ratio<1, 1000000000>>>'))
        std::cout << ::std::chrono::system_clock::now();

It works fine if I don't wrap the class in a namespace. It also works fine if I change operator<< to SomeMethod.

Upvotes: 0

Views: 84

Answers (1)

Howard Hinnant
Howard Hinnant

Reputation: 218750

Try using:

using date::operator<<;

C++ has a habit of hiding signatures in one namespace from another. But if you bring in a specific signature, such as operator<<, you can circumvent that hiding.

If this is truly a fully implemented C++20, then you don't need #include "date.h" nor using namespace date;, and your code would likely work as is. My guess is that the C++20 chrono bits are not yet fully implemented by your vendor.

Upvotes: 2

Related Questions