【问题标题】:Overloading operator<<: cannot bind lvalue to ‘std::basic_ostream<char>&&’重载运算符<<:无法将左值绑定到“std::basic_ostream<char>&&”
【发布时间】:2012-05-18 10:44:36
【问题描述】:

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

#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;
}
  • 在不支持 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]’
    

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

【问题讨论】:

    标签: c++


    【解决方案1】:

    您在此运算符中的 "non-deducible context" 有问题

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

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

    在 C++11 模式下,编译器会继续从标准库中查找匹配项

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

    它可以将_Tp 匹配到几乎任何类型,包括classA&lt;T&gt;::classB,但请注意第一个参数不匹配。

    【讨论】:

    • 关于势均力敌的评论清楚地说明了我遇到类似问题的原因。
    • 感谢您的链接,这对理解您在说什么很有帮助
    【解决方案2】:

    Bo 提供了原因为什么会发生这种情况(T 类型在调用嵌套的operator&lt;&lt; 时是不可推断的。一个简单的解决方法,我一般推荐,不仅在这里,不是与模板成为朋友,而是一个单独的免费函数。为此,您需要定义内联函数:

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

    这两种方法之间存在一些差异。最重要的是,这种方法将使编译器为模板的每个实例化 operator&lt;&lt; 定义一个非模板化重载,因为它不再是模板,所以不依赖于推导参数。另一个副作用是该方法有点更紧密(您只是与 一个 函数成为朋友,而在您最初的方法中,您与模板和所有可能的实例化(可以是用作访问类内部的漏洞)。最后,这样定义的函数只能通过 ADL 找到,因此当参数不是 ClassA&lt;T&gt; 或 @987654330 时,编译器需要考虑的 operator&lt;&lt; 重载较少@。


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

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

    另类

    或者,您可以与模板的特定专业化成为朋友。这将解决intruder 问题,因为它只对operator&lt;&lt; 到ClassA&lt;intruder&gt; 开放,影响要小得多。但这不会解决您的特定问题,因为类型仍然不可推断。

    【讨论】:

    • 我认为它是一个更好的设计,而不是一个解决方法。它有它的缺点(您不能获取在模板类中声明的友元函数的地址),但在所有其他方面,它更适合提供免费的函数运算符...
    • 这是Making New Friends 成语,所以不是真正的解决方法。
    • @TBBle: 有人给它取了个名字很好,但是那个成语已经用了很长时间了,没有名字也很好:)
    • 我并不是要暗示它一旦被命名就不再是一种解决方法。这个成语的名字至少是10 years old。据我所知,changes that made it necessary 比这大 10 年以上。虽然在当时,这个用例的例子似乎都与“预先声明”有关。
    【解决方案3】:

    试试这个:

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

    然后你会得到一个直截了当的无能供词:

    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>&)
    

    解决方法:也许您可以在嵌套的 classB 中使用成员函数,并使用它来代替 operator

    【讨论】:

    • 你总是可以显式调用::operator&lt;&lt; &lt;T&gt; (out, tree.root),但我认为命名成员函数现在看起来更有吸引力。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-27
    • 2018-03-03
    • 2022-09-28
    • 1970-01-01
    • 1970-01-01
    • 2014-05-24
    • 1970-01-01
    相关资源
    最近更新 更多