"ambiguous overload for 'operator<<'" *没有*包罗万象的重载

"ambiguous overload for 'operator<<'" *without* a catch-all overload

本文关键字:lt 重载 包罗万象 没有 operator for ambiguous overload      更新时间:2023-10-16

所以我试图实现xorshift prng作为random的参数化stl风格类,例如std::mersenne_twister_engine,所以我可以将它与random库等的那些非常方便的分布一起使用。

无论如何,我有一个超载operator<<的问题,坦率地说,我完全难住了。

类是这样参数化的:

template <size_t __n,
          int_least8_t __a, int_least8_t __b, int_least8_t __c,
          uint64_t __m>
class xorshift_engine
{
...
重载在类内部声明为friend,如下所示:
template <size_t __n_,
          int_least8_t __a_, int_least8_t __b_, int_least8_t __c_,
          uint64_t __m_,
          typename _CharT, typename _Traits>
friend std::basic_istream<_CharT, _Traits>&
operator<< (std::basic_ostream<_CharT, _Traits>& _os,
            const xorshift_engine<__n_, __a_, __b_, __c_, __m_>& _x);

及其在类外的实现是这样的:

template <size_t __n,
          int_least8_t __a, int_least8_t __b, int_least8_t __c,
          uint64_t __m,
          typename _CharT, typename _Traits>
std::basic_ostream<_CharT, _Traits>&
operator<< (std::basic_ostream<_CharT, _Traits>& _os,
            const xorshift_engine<__n, __a, __b, __c, __m>& _x)
{
    ...
}

当我尝试编译以下代码时:

#include <iostream>
#include <random>
#include "xorshift.hpp"
using namespace std;
int main()
{
    xorshift1024star bip(2345);
    mt19937_64 bip2(2345);
    cout << bip << endl;
    cout << endl << bip2 << endl;
    return 0;
}

(xorshift1024star只是xorshift_engine类的实例化:

typedef xorshift_engine<16, -31, 11, 30, 1181783497276652981ULL> xorshift1024star;

)我得到这个错误(我用---替换了文件路径中的用户名):

C:Users---Documentsrandgen.cpp: In function 'int main()':
C:Users---Documentsrandgen.cpp:11:7: error: ambiguous overload for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream<char>}' and 'xorshift1024star {aka xorshift_engine<16ull, -31, 11, 30, 1181783497276652981ull>}')
  cout << bip << endl;
       ^
C:Users---Documentsrandgen.cpp:11:7: note: candidates are:
In file included from C:Users---Documentsrandgen.cpp:3:0:
C:Users---Documentsxorshift.hpp:898:1: note: std::basic_ostream<_CharT, _Traits>& operator<<(std::basic_ostream<_CharT, _Traits>&, const xorshift_engine<__n, __a, __b, __c, __m>&) [with long long unsigned int __n = 16ull; signed char __a = -31; signed char __b = 11; signed char __c = 30; long long unsigned int __m = 1181783497276652981ull; _CharT = char; _Traits = std::char_traits<char>]
 operator<< (std::basic_ostream<_CharT, _Traits>& _os,
 ^
C:Users---Documentsxorshift.hpp:858:2: note: std::basic_istream<_CharT, _Traits>& operator<<(std::basic_ostream<_CharT, _Traits>&, const xorshift_engine<__n_, __a_, __b_, __c_, __m_>&) [with long long unsigned int __n_ = 16ull; signed char __a_ = -31; signed char __b_ = 11; signed char __c_ = 30; long long unsigned int __m_ = 1181783497276652981ull; _CharT = char; _Traits = std::char_traits<char>; long long unsigned int __n = 16ull; signed char __a = -31; signed char __b = 11; signed char __c = 30; long long unsigned int __m = 1181783497276652981ull]
  operator<< (std::basic_ostream<_CharT, _Traits>& os,
  ^
In file included from C:/mingw-w64/x86_64-4.9.0-posix-seh-rt_v3-rev1/mingw64/x86_64-w64-mingw32/include/c++/iostream:39:0,
                 from C:Users---Documentsrandgen.cpp:1:
C:/mingw-w64/x86_64-4.9.0-posix-seh-rt_v3-rev1/mingw64/x86_64-w64-mingw32/include/c++/ostream:602:5: note: std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&) [with _CharT = char; _Traits = std::char_traits<char>; _Tp = xorshift_engine<16ull, -31, 11, 30, 1181783497276652981ull>] <near match>
     operator<<(basic_ostream<_CharT, _Traits>&& __os, const _Tp& __x)
     ^
C:/mingw-w64/x86_64-4.9.0-posix-seh-rt_v3-rev1/mingw64/x86_64-w64-mingw32/include/c++/ostream:602:5: note:   no known conversion for argument 1 from 'std::ostream {aka std::basic_ostream<char>}' to 'std::basic_ostream<char>&&'
作为参考,这是bits/random.hmersenne_twister_engine内部重载声明的方式:
template<typename _UIntType1,
         size_t __w1, size_t __n1,
         size_t __m1, size_t __r1,
         _UIntType1 __a1, size_t __u1,
         _UIntType1 __d1, size_t __s1,
         _UIntType1 __b1, size_t __t1,
         _UIntType1 __c1, size_t __l1, _UIntType1 __f1,
         typename _CharT, typename _Traits>
friend std::basic_ostream<_CharT, _Traits>&
operator<<(std::basic_ostream<_CharT, _Traits>& __os,
           const std::mersenne_twister_engine<_UIntType1, __w1, __n1,
           __m1, __r1, __a1, __u1, __d1, __s1, __b1, __t1, __c1,
           __l1, __f1>& __x);

及其在bits/random.tcc中的实现:

template<typename _UIntType, size_t __w,
         size_t __n, size_t __m, size_t __r,
         _UIntType __a, size_t __u, _UIntType __d, size_t __s,
         _UIntType __b, size_t __t, _UIntType __c, size_t __l,
         _UIntType __f, typename _CharT, typename _Traits>
std::basic_ostream<_CharT, _Traits>&
operator<<(std::basic_ostream<_CharT, _Traits>& __os,
           const mersenne_twister_engine<_UIntType, __w, __n, __m,
           __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>& __x)
{
  ...
}

如果我注释掉我的测试代码中的cout << bip << endl;行,它编译和运行没有错误,所以这个过载是好的,但我的不是。

我完全没有主意了。我错过了什么?如果有人能帮助我,我将不胜感激。

edit:对于那些建议这是"模板朋友"问题,我应该通过前向声明函数来解决,我刚刚添加了

template <size_t __n,
          int_least8_t __a, int_least8_t __b, int_least8_t __c,
          uint64_t __m>
class xorshift_engine;
template <size_t __n,
          int_least8_t __a, int_least8_t __b, int_least8_t __c,
          uint64_t __m,
          typename _CharT, typename _Traits>
std::basic_ostream<_CharT, _Traits>&
operator<< (std::basic_ostream<_CharT, _Traits>& _os,
            const xorshift_engine<__n, __a, __b, __c, __m>& _x);

在类体/实现之前,并且在尝试编译时得到相同的错误。


edit2:给出相同编译错误的简化示例:

//file "failclass.hpp"
#ifndef _FAILCLASS_HPP
#define _FAILCLASS_HPP
#include <iosfwd>
#include <type_traits>
template <int n>
class failclass
{
private:
    static constexpr int k = n;
public:
    template<int n1, typename _CharT, typename _Traits>
    friend std::basic_istream<_CharT, _Traits>&
    operator<< (std::basic_ostream<_CharT, _Traits>& _os,
                const failclass<n1>& _x);
};
template<int n1, typename _CharT, typename _Traits>
std::basic_ostream<_CharT, _Traits>&
operator<< (std::basic_ostream<_CharT, _Traits>& _os,
            const failclass<n1>& _x)
{
    _os << _x.k;
    return _os;
}
#endif // _FAILCLASS_HPP

试着编译这个:

//file "failtest.cpp"
#include <iostream>
#include "failclass.hpp"
using namespace std;
int main()
{
    failclass<5> a;
    cout << a;
    return 0;
}

将答案的其余部分保留为其值,但特定的错误只是一个错别字:

template <size_t __n_,
              int_least8_t __a_, int_least8_t __b_, int_least8_t __c_,
              uint64_t __m_,
              typename _CharT, typename _Traits>
    friend std::basic_istream<_CharT, _Traits>&
//                    ^     -- you probably meant std::ostream!!!!!
    operator<< (std::basic_ostream<_CharT, _Traits>& _os,
                const xorshift_engine<__n_, __a_, __b_, __c_, __m_>& _x);

在命名空间级别定义的模板和友元使用完全相同的实参,但返回类型不同。


下面的大部分并不是对错误消息的直接回答,但是解决了使你陷入错误的根本问题。我个人认为远期申报应该已经解决了。如果没有,您应该提供SCCE。

为了便于讨论,让我们将代码简化为一个模板,其中只有一个参数,您希望实现operator<<。友元声明:

template <typename T>
class Tmpl {
    friend std::ostream& operator<<(std::ostream&, Tmpl const &);
};

非模板独立函数operator<<提供声明,该函数接受一个std::ostream&和一个Tmpl<T> const &。这里有一个重要的细节,这不是模板,也不是任何自由函数。给定一个专门化Tmpl<int>,该声明在相同的命名空间中用以下签名将一个函数定义为朋友:

std::ostream& operator<<(std::ostream& out, Tmpl<int> const & obj) {
   // I can access Tmpl<int> internals!
   return out;
}
虽然这是一种可能的方法,但您很可能不希望必须为模板的每个专门化手动提供一个不同的自由函数。在这一点上,friend声明有一个非常有趣的特性:您可以在类中提供定义,以及友元声明:
template <typename T>
class Tmpl {
    friend std::ostream& operator<<(std::ostream out, Tmpl const & t) {
       // definition goes here
       return out;
    }
};
这里有趣的一点是,对于Tmpl的每个特化,编译器将为您生成一个非模板自由函数,该函数可以访问该类型的内部。

现在,在这种特殊情况下,您可能需要考虑其他选择。我经常使用它,首先想到的不是让operator<<成为朋友,而是提供一个print函数(您可以向其添加其他参数来控制输出),然后通过调用print来根据公共接口实现operator<<。您可以选择仍然使operator<<成为在类[1]中定义的朋友,或者您可以提供一个模板化的operator<<,它调用print:

template <typename T>
class Tmpl {
public:
   std::ostream& print(std::ostream& out) const;
   // option 1:
   friend std::ostream& operator<<(std::ostream& out, Tmpl const & obj) {
       return obj.print(out);
   }
};
// option 2:
template <typename T>
std::ostream& operator<<(std::ostream& out, Tmpl<T> const & obj) {
   return obj.print(out);
}
另一种替代方法(我不推荐,但为了完整起见)是将模板声明为友元:
template <typename T>
class Tmpl {
public:
   template <typename U>
   friend std::ostream& operator<<(std::ostream& out, Tmpl<U> const & obj);
};
// template defined as option 2 above

但这是一个坏的想法(以及您选择的替代方案),因为operator<< <int>将访问Tmpl<double>,并且很容易破坏封装。

一个稍微好一点的替代方法是只对上述模板的专门化与实参完全匹配,但这在代码中有点复杂:

template <typename T> class Tmpl;
template <typename T>
std::ostream& operator<<(std::ostream&, Tmpl<T> const &);
template <typename T>
class Tmpl {
    friend std::ostream& operator<< <T>(std::ostream&, Tmpl<T> const &);
};

1仍然使operator<<为友元的可能原因是,即使它仅在公共接口方面实现,它操作符从正常查找中隐藏,并使其仅对ADL可用。