vendredi 9 avril 2021

Why is operator<< not found if defined in namespace?

In the code below, I get an error when trying to print time_point to the console. It goes away if I move operator<< outside of my_namespace. Why is this? Must I define my operator<< function in the global namespace?

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

namespace my_namespace {

std::ostream &operator<<(std::ostream &os,
                         const std::chrono::system_clock::time_point &time_point) {
  return os << date::format(
      "%F %T\n",
      std::chrono::time_point_cast<std::chrono::milliseconds>(time_point));
}

}

int main() {

  std::chrono::system_clock::time_point time_point{
      std::chrono::milliseconds{1000}};
  std::cout << time_point;

}

Aucun commentaire:

Enregistrer un commentaire