C++ 重载运算符<<:无法将左值绑定到“std::basic_ostream<char>&&”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10651161/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 14:18:10  来源:igfitidea点击:

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

c++

提问by Antoine

I have a class that uses a nested class, and want to use the nested class operator<<to define operator<<in the upper class. Here is how my code looks like:

我有一个使用嵌套类的类,并且想使用嵌套类在上层类中operator<<定义operator<<。这是我的代码的样子:

#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:

    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:

    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]'
    
  • 在不支持 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: ...
    
  • 使用 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?

有人能告诉我为什么这个代码不合法​​,做我想做的最好的方法是什么?

采纳答案by David Rodríguez - dribeas

Bo provided the reasonwhy this is happening (the type Tis 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 definethe 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. Another side effects are that the approach is a little tighter(you are only befriending onefunction, 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.

这两种方法之间存在一些差异。最重要的是,这种方法会让编译器为模板operator<<的每个实例化定义一个非模板化的重载,因为它不再是模板,因此不依赖于推导参数。另一个副作用是该方法有点严格(您只与一个函数成为朋友,而在您的初始方法中,您与模板和所有可能的实例化(可用作访问类内部的漏洞)成为朋友。最后。如此定义的函数只能通过 ADL 找到,因此operator<<当参数不是ClassA<T>or时编译器要考虑的重载较少ClassA<T>::ClassB



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 intruderproblem, as it will only be open to operator<<to ClassA<intruder>, which has a much lesser impact. But this will not solve your particular issue, as the type would still not be deducible.

或者,您可以与模板的特定专业成为朋友。这将解决这个intruder问题,因为它只会开放给operator<<ClassA<intruder>,其中有一个很大的影响较小。但这并不能解决您的特定问题,因为该类型仍然不可推导。

回答by Bo Persson

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 Twill result in a classBthat matches the parameter you want to pass. So this template is not considered!

编译器无法确定哪些值T将导致classB与您要传递的参数匹配的a 。所以不考虑这个模板!

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 canmatch _Tpto just about any type, including classA<T>::classB, but notes that the first parameter doesn't match.

可以匹配_Tp几乎任何类型,包括classA<T>::classB,但请注意第一个参数不匹配。

回答by dsign

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 中使用成员函数,并使用它代替 operator<< ... 当然,该解决方案有很多缺点,但它可能会让您摆脱这种匆忙。