相关疑难解决方法(0)

模板化运算符重载决策,成员与非成员函数

在尝试使用clang-3.4(从git编译)时,它无法编译我的一个项目,抱怨在解决重载运算符时出现歧义.我发现有两个模板化运算符,其中一个被声明为成员函数,另一个被称为非成员函数,它们似乎都是同样好的匹配.

继SSCCE之后展示了这种情况:

#include <iostream>

struct ostr {
        std::ostream& s;

        template<class T>
        ostr& operator<<(const T& x) { s << x; return *this; }
};

struct xy {
        double x, y;
};

template<class Stream>
Stream& operator<<(Stream& s, const xy& x) {
        s << "[" << x.x << ", " << x.y << "]";
        return s;
}

int main() {
        ostr os{std::cout};
        xy x{4, 5};
        os << "Value is: " << x <<"\n";
}
Run Code Online (Sandbox Code Playgroud)

该项目之前编译很好,我再次检查这个SSCCE几个编译器(gcc 4.5,4.6,4.7,4.8 …

c++ templates clang++

6
推荐指数
1
解决办法
475
查看次数

标签 统计

c++ ×1

clang++ ×1

templates ×1