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

Ant*_*ine 42 c++

我有一个使用嵌套类的类,并希望使用嵌套类在上层类中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;
}
Run Code Online (Sandbox Code Playgroud)
  • 在不支持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: ...
    
    Run Code Online (Sandbox Code Playgroud)
  • 使用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]’
    
    Run Code Online (Sandbox Code Playgroud)

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

Bo *_*son 28

您在此运算符中遇到"不可导入的上下文"问题

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

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

在C++ 11模式下,编译器继续从标准库中找到一个紧密匹配

operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&)
Run Code Online (Sandbox Code Playgroud)

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


Dav*_*eas 23

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
  }
};
Run Code Online (Sandbox Code Playgroud)

这两种方法有两点不同.最重要的一点是,这种方法将使编译器为模板operator<<的每个实例化定义一个非模板化的重载,因为它不再是模板,不依赖于推导参数.另一个副作用是方法更紧凑(你只是与一个函数交朋友,而在你最初的方法中,你结识了模板和所有可能的实例化(可以作为漏洞来获得对你的类内部的访问).如此定义的函数只能通过ADL找到,因此operator<<当参数不是ClassA<T>或时,编译器需要考虑的重载次数较少ClassA<T>::ClassB.


如何通过您的方法获得访问权限

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 _;
   }
}
Run Code Online (Sandbox Code Playgroud)

替代

或者,您可以与模板的特定专业化建立联系.这将解决这个intruder问题,因为它只会开放给operator<<ClassA<intruder>,其中有一个很大的影响较小.但这不会解决您的特定问题,因为该类型仍然无法推断.