简体   繁体   English

重载operator <<:无法将左值绑定到'std :: basic_ostream <char> &&'

[英]Overloading operator<<: cannot bind lvalue to ‘std::basic_ostream<char>&&’

I have a class that uses a nested class, and want to use the nested class operator<< to define operator<< in the upper class. 我有一个使用嵌套类的类,并希望使用嵌套类operator<< operator<<在上层类中定义operator<< Here is how my code looks like: 以下是我的代码的样子:

#include <memory>
#include <iostream>

template<typename T>
struct classA {
  struct classB
  {
    template<typename U>
    friend inline std::ostream& operator<< (std::ostream &out,
                                            const typename classA<U>::classB &b);
  };

  classB root;

  template<typename U>
  friend std::ostream& operator<< (std::ostream &out,
                                   const classA<U> &tree);
};

template<typename T>
inline std::ostream& operator<< (std::ostream &out,
                                 const classA<T> &tree)
{
  out << tree.root;
  return out;
}

template<typename T>
inline std::ostream& operator<< (std::ostream &out,
                                 const typename classA<T>::classB &b)
{
  return out;
}

int main()
{
  classA<int> a;
  std::cout << a;
}
  • When compiling without support for C++11, the definition of operator<< for the inner class seems not to be found by the compiler: 在不支持C ++ 11的情况下进行编译时,编译器似乎找不到内部类的operator <<的定义:

     so.hpp:24:7: error: no match for 'operator<<' in 'out << tree.classA<int>::root' so.hpp:24:7: note: candidates are: ... 
  • With GCC 4.6 and 4.7 when compiling with std=c++0x: 使用std = c ++ 0x进行编译时使用GCC 4.6和4.7:

     so.hpp:21:3: error: cannot bind 'std::ostream {aka std::basic_ostream<char>}' lvalue to 'std::basic_ostream<char>&&' In file included from /usr/include/c++/4.7/iostream:40:0, from so.hpp:2: /usr/include/c++/4.7/ostream:600:5: error: initializing argument 1 of 'std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&) [with _CharT = char; _Traits = std::char_traits<char>; _Tp = classA<int>::classB]' 

Can someone tell me why this code is not legal, and what's the best way to do what I want? 有人能告诉我为什么这段代码不合法​​,什么是做我想要的最好的方法?

You have a problem with a "non-deducible context" in this operator 您在此运算符中遇到“不可导入的上下文”问题

template<typename T>
inline std::ostream& operator<< (std::ostream &out,
                                 const typename classA<T>::classB &b)
{
  return out;
}

The compiler cannot figure out what values of T will result in a classB that matches the parameter you want to pass. 编译器无法确定哪个T值会导致classB与您要传递的参数匹配。 So this template is not considered! 所以这个模板不予考虑!

In C++11 mode, the compiler then goes on to find a close match from the standard library 在C ++ 11模式下,编译器继续从标准库中找到一个紧密匹配

operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&)

where it can match _Tp to just about any type, including classA<T>::classB , but notes that the first parameter doesn't match. 可以匹配_Tp几乎任何类型,包括classA<T>::classB ,但注意第一个参数不匹配。

Bo provided the reason why this is happening (the type T is not deducible in the call to the nested operator<< . A simple workaround for this, and something that I recommend in general, not only here, is not befriending a template, but rather a single free function. For that you will need to define the function inline: Bo提供了为什么会发生这种情况的原因 (类型T在嵌套operator<<的调用中是不可推断的。这是一个简单的解决方法,我推荐的一般情况,不仅仅是在这里,不是模板的朋友,而是而是一个单独的自由函数。为此你需要定义内联函数:

template<typename T>
struct classA {
  struct classB
  {
    friend inline std::ostream& operator<< (std::ostream &out,
                                            const classB &b) {
       // definition goes here
    }
  };

  classB root;

  friend std::ostream& operator<< (std::ostream &out,
                                   const classA<U> &tree) {
       // definition goes here
  }
};

There are a couple of differences among the two approaches. 这两种方法有两点不同。 The most important one is that this approach will have the compiler define a non-templated overload for operator<< for each instantiation of the template, which because it is no longer a template, does not depend on deducing the arguments. 最重要的一点是,这种方法将使编译器为模板的每个实例化定义operator<<的非模板化重载,因为它不再是模板,不依赖于推导参数。 Another side effects are that the approach is a little tighter (you are only befriending one function, while in your initial approach you befriended the template and all possible instantiations (which can be used as a loophole to gain access to your class internals). Finally the functions so defined will only be found through ADL, so there are less overloads of operator<< for the compiler to consider when the argument is not ClassA<T> or ClassA<T>::ClassB . 另一个副作用是方法更紧凑 (你只是与一个函数交朋友,而在你最初的方法中,你结识了模板和所有可能的实例化(可以作为漏洞来获得对你的类内部的访问)。这样定义的函数只能通过ADL找到,因此当参数不是ClassA<T>ClassA<T>::ClassB时,编译器要考虑的operator<<重载次数较少。


How access can be gained with your approach 如何通过您的方法获得访问权限

namespace {
   struct intruder {
       ClassA & ref;
       intruder( ClassA& r ) : ref(r) {}
   };
   template <>
   std::ostream& operator<< <intruder>( std::ostream& _, ClassA<intruder> const& i ) {
       std::cout << i.ref.private_member << std::endl;
       return _;
   }
}

Alternative 替代

Alternatively you can befriend a particular specialization of a template. 或者,您可以与模板的特定专业化建立联系。 That will solve the intruder problem, as it will only be open to operator<< to ClassA<intruder> , which has a much lesser impact. 这将解决intruder问题,因为它只对operator<< to ClassA<intruder> ,后者的影响要小得多。 But this will not solve your particular issue, as the type would still not be deducible. 但这不会解决您的特定问题,因为该类型仍然无法推断。

Try this: 试试这个:

template<typename T>
inline std::ostream& operator<< (std::ostream &out,
                             const classA<T> &tree)
{
   //out << tree.root;
   ::operator<<( out, tree.root);
   return out;
}

and then you will get a straightforward confession of ineptitude: 然后你会得到一个直率的无能告白:

test.cpp:34:3: error: no matching function for call to ‘operator<<(std::ostream&, const classA<int>::classB&)’
test.cpp:34:3: note: candidates are:
test.cpp:23:22: note: template<class T> std::ostream& operator<<(std::ostream&, const     typename classA<T>::classB&)
test.cpp:30:22: note: template<class T> std::ostream& operator<<(std::ostream&, const classA<T>&)

Workaround: maybe you can use a member function in nested classB, and use it instead of operator<< ... Of course, that solution has a multitude of drawbacks, but it may get you out of this hurry. 解决方法:也许您可以在嵌套的classB中使用成员函数,并使用它而不是运算符<< ...当然,该解决方案有许多缺点,但它可能会让您摆脱这种匆忙。

暂无
暂无

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

相关问题 重载运算符&lt;&lt;:无法绑定&#39;std :: basic_ostream <char> &#39;左值到&#39;std :: basic_ostream <char> &amp;&amp;&#39; - Overloading operator<<: cannot bind ‘std::basic_ostream<char>’ lvalue to ‘std::basic_ostream<char>&&’ 重载运算符&lt;&lt;无法绑定basic_ostream <char> 左值 - Overloading operator<< Cannot bind basic_ostream<char> lvalue 无法绑定&#39;std :: ostream {aka std :: basic_ostream <char> }&#39;左值成&#39;std :: basic_ostream <char> &amp;&amp;” - cannot bind 'std::ostream {aka std::basic_ostream<char>}' lvalue to 'std::basic_ostream<char>&&' 错误:无法绑定&#39;std :: basic_ostream <char> &#39;左右&#39;std :: basic_ostream <char> &amp;&amp;” - error: cannot bind ‘std::basic_ostream<char>’ lvalue to ‘std::basic_ostream<char>&&’ std :: vector:无法绑定&#39;std :: ostream {aka std :: basic_ostream <char> &#39;&#39;左右&#39;到&#39;std :: basic_ostream <char> &amp;&amp;” - std::vector : cannot bind 'std::ostream {aka std::basic_ostream<char>}' lvalue to 'std::basic_ostream<char>&&' 错误:无法绑定&#39;std :: ostream {aka std :: basic_ostream <char> }&#39;左值成&#39;std :: basic_ostream <char> &amp;&amp;&#39; - error: cannot bind ‘std::ostream {aka std::basic_ostream<char>}’ lvalue to ‘std::basic_ostream<char>&&’ high_resolution_clock错误:无法绑定&#39;std :: ostream {aka std :: basic_ostream <char> }&#39;左值成&#39;std :: basic_ostream <char> &amp;&amp;&#39; - high_resolution_clock error: cannot bind ‘std::ostream {aka std::basic_ostream<char>}’ lvalue to ‘std::basic_ostream<char>&&’ 错误:无法将 'std::basic_ostream' 左值绑定到 'std::basic_ostream&amp;&amp;' sl &lt;&lt; ss; - error: cannot bind ‘std::basic_ostream’ lvalue to ‘std::basic_ostream&&’ sl << ss; boost :: PO并且不能绑定到&#39;std :: basic_ostream <char> &amp;&amp;” - boost::PO and cannot bind to ‘std::basic_ostream<char>&&’ 重载运算符&gt;&gt;:无法绑定&#39;std::basic_istream<char> &#39; 左值到 &#39;std::basic_istream<char> &amp;&amp;&#39; - Overloading operator>>: cannot bind ‘std::basic_istream<char>’ lvalue to ‘std::basic_istream<char>&&’
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM