不匹配'operator

问题描述 投票:1回答:1

实际上,我正在尝试将访客模式与某些模板一起使用。

我想解析包含unordered_maptype_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;

这里是我的循环谁不编译

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");
    }
  }
};

[如果有人对如何解决它有想法,我会很乐意接受!谢谢

c++ loops unordered-map visitor
1个回答
0
投票

您应该尝试打印i.first.name(),而不是仅打印i.first

© www.soinside.com 2019 - 2024. All rights reserved.