简体   繁体   中英

Ambiguous overload of std::ostream& operator<<(std::ostream& sstr, const T& val)

In order to serialize any object (ie for objects without a global ostream& operator<< the string is empty), I've create a function which utilizes an overloaded std::ostream& operator<< from a separate namespace;

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

This works for all types I've tried, except char's where operator<< is considered ambiguous. I can't figure why it works for int's, short's or any other type which has the operator defined, but not for chars. Anyone have any ideas?

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 ==========

The compiler seems to place the casting from const char& to char on the same level as upcasting std::ostringstream to std::ostream when the overload resolution comes.

The solution could be to template the type of operator<< to avoid the upcasting:

namespace _impl {
    template <typename T, typename Y>
    Y& operator<<(Y& osstr, const T& val) {
      return osstr;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM