C++ std的模糊过载::ostream&;操作员<&书信电报;(标准:ostream&;sstr、const T&;val)

C++ std的模糊过载::ostream&;操作员<&书信电报;(标准:ostream&;sstr、const T&;val),c++,templates,operator-overloading,C++,Templates,Operator Overloading,为了序列化任何对象(例如,对于没有全局ostream&operatorapplication\u src\general\u experiments.cpp(39):错误C2593:“operator当重载解析到来时,编译器似乎将从const char&到char的转换放在与向上转换std::ostringstream相同的级别上 解决方案可能是模板化运算符的类型是。。。尽管这不是一个解决方案:(不起作用,编译器似乎优先于所有其他运算符 namespace _impl { template &l

为了序列化任何对象(例如,对于没有全局
ostream&operatorprecode1>application\u src\general\u experiments.cpp(39):错误C2593:“operator当重载解析到来时,编译器似乎将从
const char&
char
的转换放在与向上转换
std::ostringstream
相同的级别上


解决方案可能是模板化
运算符的类型是。。。尽管这不是一个解决方案:(不起作用,编译器似乎优先于所有其他运算符
namespace _impl {
template <typename T>
std::ostream& operator<<(std::ostream& osstr, const T& val) {
  return osstr;
}
}

template <typename T>
std::string serialize_any(const T& val) {
  using namespace _impl;
  std::ostringstream osstr;
  osstr<< val;
  std::string str(osstr.str());
  return str;
}
1>application_src\general_experiments.cpp(39): error C2593: 'operator <<' is ambiguous
1>          application_src\general_experiments.cpp(26): could be 'std::ostream &_impl::operator <<<T>(std::ostream &,const T &)'
1>          with
1>          [
1>              T=char
1>          ]
1>          C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\ostream(914): or       'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,_Elem)' [found using argument-dependent lookup]
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\ostream(827): or       'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,char)' [found using argument-dependent lookup]
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\ostream(742): or       'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,char)' [found using argument-dependent lookup]
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          while trying to match the argument list '(std::ostringstream, const char)'
1>          application_src\general_experiments.cpp(52) : see reference to function template instantiation 'std::string serialize_any<char>(const T &)' being compiled
1>          with
1>          [
1>              T=char
1>          ]
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
namespace _impl {
    template <typename T, typename Y>
    Y& operator<<(Y& osstr, const T& val) {
      return osstr;
    }
}