plog icon indicating copy to clipboard operation
plog copied to clipboard

plog cannot find user's custom operator<< from user's namespace.

Open ghost opened this issue 6 years ago • 3 comments

plog::Record might break some idiom code like the following example. Without using mine::operator<<; the p_log function would get a compiler error no match for ‘operator<<’ (operand types are ‘plog::util::nostringstream’ {aka ‘std::__cxx11::basic_ostringstream<char>’} and ‘const third_party::A’). c_log and ss_log work fine anyway.


#include <plog/Log.h>
#include <iostream>
#include <plog/Appenders/ConsoleAppender.h>

namespace third_party {
struct A {
  int a;
};
} // namespace third_party

namespace mine {
std::ostream& operator<<(std::ostream& os, const third_party::A& a) {
  return os << "mine::operator<< A" << a.a;
}

void c_log(const third_party::A& a) { std::clog << "c_log A{" << a << "}\n"; }

void ss_log(const third_party::A& a) {
  std::ostringstream oss;
  oss << "ss_log A{" << a << "}\n";
  std::clog << oss.str();
}

void p_log(const third_party::A& a) { LOGI << "p_log A{" << a << "}"; }
} // namespace mine

namespace plog {
//using mine::operator<<;
} // namespace plog

int main() {
  plog::ConsoleAppender<plog::TxtFormatter> s;
  plog::init(plog::verbose, &s);

  third_party::A a = {42};
  mine::c_log(a);
  mine::ss_log(a);
  mine::p_log(a);
  return 0;
}

ghost avatar Jan 16 '19 06:01 ghost

The issue is confirmed. Thanks for a good report.

SergiusTheBest avatar Jan 17 '19 08:01 SergiusTheBest

Hi, is this issue resolved?? is there any workaround for this? is this issue because of Visual Studio 2017?

bhatiahs avatar Nov 26 '19 05:11 bhatiahs

Hi @bhatiahs ,

No, it's still actual and it's not related to VS2017. A workaround is suggested by the issue author:

namespace plog {
    using mine::operator<<;
}

SergiusTheBest avatar Nov 26 '19 12:11 SergiusTheBest