c++ - 'operator<<' 不匹配(操作数类型为 'std::ostream' {aka 'std::basic_ostream'} 和 'const std::type_index')
问题描述
实际上,我正在尝试将访问者模式与一些模板一起使用。
我想解析我unordered_map
的包含type_index
和function
变量,但是我得到一个编译错误,即使在阅读了很多关于它的主题之后我也不明白。
错误:'operator<<' 不匹配(操作数类型是 'std::ostream' {aka 'std::basic_ostream'} 和 'const std::type_index')std::cout << i.first << i .second << std::endl;
这是我无法编译的循环
for (auto i : functions) {
std::cout << i.first << i.second << std::endl;
}
这是我的代码片段和我的循环来解析和显示里面的内容unordered_map
template <typename TReturn> struct AnyVisitor {
using typeInfoRef = std::reference_wrapper<const std::type_info>;
using function = std::function<TReturn(std::any &)>;
std::unordered_map<std::type_index, function> functions;
template <typename TArg> void accept(std::function<TReturn(TArg &)> f) {
functions.insert(std::make_pair(std::type_index(typeid(TArg)),
function([&f](std::any &x) -> TReturn {
return f(std::any_cast<TArg &>(x));
})));
for (auto i : functions) {
std::cout << i.first << i.second << std::endl;
}
}
TReturn operator()(std::any &x) {
try {
auto function = functions.at(std::type_index(x.type()));
return function(x);
} catch (...) {
throw std::runtime_error("No visitor registered");
}
}
};
如果有人知道如何解决它,我很乐意接受!谢谢
解决方案
您应该尝试打印i.first.name()
而不是i.first
仅打印
推荐阅读
- azure - 如何使用服务主体连接到 Azure Databricks?
- python - 使用 vips 逐行比较图像
- android - 使用场景形式在图像上渲染视频
- laravel - 如何在 laravel 中分组和计算数据透视表?
- flutter - 异步加载广告
- apache-kafka - Kafka 代理中断/故障转移后是否总是需要重新启动流
- python - 我哪里错了(AttributeError)
- python - 将数据从 HTML 导出到 Excel
- mongodb - docker-compose mongodb 新实例不起作用
- php - json解析错误意外eof土耳其字符