简体   繁体   English

std :: ostream&运算符<<(std :: ostream&sstr,const T&val)的模棱两可的重载

[英]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; 为了序列化任何对象(即,对于没有全局ostream& operator<<的对象,字符串为空),我创建了一个函数,该函数利用来自单独命名空间的重载std::ostream& operator<<

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. 这适用于我尝试过的所有类型,但char的运算符<<被认为是模棱两可的除外。 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. 我不知道为什么它适用于int,short或定义了运算符的任何其他类型,但不适用于char。 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. 当过载解决方案到来时,编译器似乎将从const char&char放在与将std::ostringstreamstd::ostream相同的级别上。

The solution could be to template the type of operator<< to avoid the upcasting: 解决方案可能是对operator<<的类型进行模板化,以避免冲突:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 std::ostream&amp; operator&lt;&lt;(std::ostream&amp;, const T&amp;) 未被覆盖 - std::ostream& operator<<(std::ostream&, const T&) not being overridden &#39;运算符&lt;&lt;(std :: ostream&,int&)&#39;不明确 - 'operator<<(std::ostream&, int&)’ is ambiguous 错误:“对运算符&lt;&lt;(std :: ostream&,Dogs const&)的未定义引用” - Error: “Undefined reference to operator<<(std::ostream&, Dogs const&)” std :: ostream&运算符&lt;&lt;类型推导 - std::ostream& operator<< type deduction 我们可以重载 operator&lt;&lt;,第一个参数的类型是 std::ostream&amp;&amp; 而不是 std::ostream&amp; - Can we overload operator<< with the first parameter being of the type std::ostream&& instead of std::ostream& “friend std::ostream&amp; operator&lt;&lt;(std::ostream&amp; out, LinkedList&amp; list)”是什么意思? - What does “friend std::ostream& operator<<(std::ostream& out, LinkedList& list)” mean? “对&#39;std::operator&lt;&lt;(std::ostream&amp;, std::LinkedList const&amp;)的未定义引用”C++ - "Undefined reference to 'std::operator<<(std::ostream&, std::LinkedList const&)" C++ ostream&运算符&lt;&lt;(ostream&(* pf)(ostream&)); - ostream& operator<< (ostream& (*pf)(ostream&)); 如何在std:ostream&member中正确使用,而“this”是const? - how to use use properly in std:ostream& member, while “this” is a const? 重载运算符std :: ostream&operator &lt;&lt;打印实例内存地址 - Overloaded operator std::ostream& operator<< prints instance memory address
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM