[英]No match for ‘operator<<’ (operand types are ‘std::ostream’ {aka ‘std::basic_ostream<char>’} and ‘const std::type_index’)
Actually, I'm trying to use the visitor pattern with some templates.实际上,我正在尝试将访问者模式与一些模板一起使用。
I want to parse my unordered_map
wich contains type_index
and the function
variable but I get a compilation error that I don't understand even after reading a lot of topics about it.我想解析我的unordered_map
包含type_index
和function
变量,但我得到一个编译错误,即使在阅读了很多关于它的主题后我也不明白。
error: no match for ‘operator<<’ (operand types are ‘std::ostream’ {aka ‘std::basic_ostream<char>’} and ‘const std::type_index’)
std::cout << i.first << i.second << std::endl;
Here is my loop who doesn't compile这是我没有编译的循环
for (auto i : functions) {
std::cout << i.first << i.second << std::endl;
}
Here is my code snippet with my loop to parse and display what is inside the unordered_map
这是我的代码片段和我的循环来解析和显示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");
}
}
};
If anyone has an idea of how to resolve it, I'll gladly take it !如果有人知道如何解决它,我很乐意接受! Thanks谢谢
You should try to print i.first.name()
instead of i.first
only您应该尝试打印i.first.name()
而不是i.first
only
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.