使用std::vector的流运算符进行参数相关查找



我已经在我的命名空间中为std::vector创建了一个流输出运算符,并且我能够在以前的调试代码中使用它:

namespace my_company {
template <typename T>
std::ostream &operator<<(std::ostream &os, std::vector<T> const &vec) {
os << "{";
for (int i = 0; i < vec.size(); ++i) {
if (i != 0) {
os << ", ";
}
os << vec[i];
}
os << "}";
return os;
}
template <typename T>
void debug_impl(T const &var, char const *const expr, char const *const file, int const line) {
using std::vector;
std::lock_guard lg(debug_print_mutex);
std::cout << "DEBUG " << file << ":" << line << " T" << std::this_thread::get_id() << "n  " << expr << ": "
<< var << std::endl;}
#define DEBUG(var) ::my_company::debug_impl(var, #var, __FILE__, __LINE__)
}

但现在我想使用我的记录器类:

namespace my_company {
class Logger {
public:
virtual ~Logger() = default;
virtual void log_impl(LogLevel log_level, std::string message) = 0;
template <typename... T>
void log_bits(LogLevel log_level, T const &...ts) {
if (log_level >= threshold)
log_impl(log_level, format_bits(ts...));
}

template <typename... T>
void trace(T const &...ts) {
log_bits(LogLevel::trace, ts...);
}
private:
LogLevel threshold = LogLevel::trace;
};
}

格式功能是这样的:

namespace my_company {
template <typename... T>
std::string format_bits(T const &...ts) {
std::ostringstream oss;
// The following is a C++17 [fold expression](https://en.cppreference.com/w/cpp/language/fold).
(oss << ... << ts);
return oss.str();
}
}

debug_impl函数中,我现在只委托给记录器:

namespace my_company {
template <typename T>
void debug_impl(T const &var, char const *const expr, char const *const file, int const line) {
logger->template trace(file, ":", line, " T", std::this_thread::get_id(), "n  ", expr, ": ", var);
}
}

Clang不喜欢这样:

logger.h:29:11: error: call to function 'operator<<' that is neither visible in the template definition nor found by argument-dependent lookup
(oss << ... << ts);
^
logger.h:44:27: note: in instantiation of function template specialization 'my_company::format_bits<const char *, char [2], int, char [3], std::thread::id, char [4], const char *, char [3], std::vector<bool, std::allocator<bool>>>' requested here
log_impl(log_level, format_bits(ts...));
^
logger.h:69:5: note: in instantiation of function template specialization 'my_company::Logger::log_bits<const char *, char [2], int, char [3], std::thread::id, char [4], const char *, char [3], std::vector<bool, std::allocator<bool>>>' requested here
log_bits(LogLevel::trace, ts...);
^
util.h:693:20: note: in instantiation of function template specialization 'my_company::Logger::trace<const char *, char [2], int, char [3], std::thread::id, char [4], const char *, char [3], std::vector<bool, std::allocator<bool>>>' requested here
logger->template trace(file, ":", line, " T", std::this_thread::get_id(), "n  ", expr, ": ", var);
^
rotating_buffer.h:40:5: note: in instantiation of function template specialization 'my_company::debug_impl<std::vector<bool, std::allocator<bool>>>' requested here
DEBUG(used);
^
util.h:699:42: note: expanded from macro 'DEBUG'
#define DEBUG(var) ::my_company::debug_impl(var, #var, __FILE__, __LINE__)
^
logger.h:13:15: note: 'operator<<' should be declared prior to the call site
std::ostream &operator<<(std::ostream &os, std::vector<T> const &vec) {
^

我已经声明,在调用站点之前,它都是在logger.h文件中声明的,该文件由其他人包含。operator<<是该文件中的第一个内容,因此它应该是可用的。我想,额外的模板层并没有让它变得更容易。

我读过一些其他问题,但我不明白如何解决这个问题,因为不允许将operator<<放入std,但它在my_company中不起作用,将其放入全局命名空间也不起作用。我能做什么?


我们在Linux上使用Clang 11和C++20。


一些相关信息:

  • 依赖于参数的查找和流运算符重载
  • 基本/STL类型/类的输出流运算符参数相关查找(ADL(

这似乎是一个clang 11错误。

Clang 12接受代码Demo。

在解决问题时,您可以使用,而不是(oss << ... << ts);:

((oss << ts), ...);

最新更新