小编Bin*_*ngo的帖子

模板运算符<<的重载解析不符合预期

问题一

给出这里的代码示例:

#include <iostream>
#include <string>

class LogStream {
public:
    LogStream& operator<<(int x) {
        std::cout << x;
        return *this;
    }
    LogStream& operator<<(const char* src) {
        std::cout << src;
        return *this;
    }
};

typedef char MyType[81];

template <typename OS>
OS& operator<<(OS &os, const MyType& data) {
  return os << "my version: " << data;
}

// error: use of overloaded operator '<<' is ambiguous
//        (with operand types 'LogStream' and 'char const[81]')

/* LogStream& operator<<(LogStream &os, const MyType& data) {
  return …
Run Code Online (Sandbox Code Playgroud)

c++ templates operator-overloading overload-resolution

5
推荐指数
1
解决办法
217
查看次数