假设我有一个类,我想根据枚举类型重载一个运算符:
#include <iostream>
enum class option : char { normal, do_something_stupid };
class foo
{
public:
int i;
explicit foo(int a=0) : i(a) {};
/* overload operator '+=' based on 'option' */
template<option E = option::normal>
void operator+=(const foo& f) { i += f.i; }
};
/* explicit specialization for operator += */
template<> void foo::operator+=<option::do_something_stupid>(const foo& f)
{ i += (f.i +1000); }
int main()
{
foo f1(1), f2(2);
f1 += f2;
std::cout << "nf1 = " << f1.i;
f1.operator+=<option::do_something_stupid>(f2);
std::cout << "nf1 = " << f1.i;
std::cout << "n";
return 0;
}
这在 g++ 和 clang++ 上都构建干净(忽略它确实做了一些非常转储的事实)。
如果我想以同样的方式重载"<<"运算符怎么办?类似的方法似乎不起作用:
#include <ostream>
#include <iostream>
enum class option : char { normal, do_something_stupid };
class foo
{
public:
int i;
explicit foo(int a=0) : i(a) {};
template<option E = option::normal>
friend std::ostream& operator<<(std::ostream& o, const foo& f)
{ o << f.i; return o; }
};
template<> std::ostream&
operator<< <option::do_something_stupid>(std::ostream& o, const foo& f)
{
o << f.i + 1000;
return o;
}
int main()
{
foo f1(1), f2(2);
std::cout << "nf1= " << f1;
std::cout << "nf2= ";
/* this triggers an error with g++ */
std::cout.operator<< <option::do_something_stupid>(f1);
std::cout << "n";
return 0;
}
根据 g++,从 main 到运算符的调用是无效的:
error: no match for ‘operator<’ (operand types are ‘<unresolved overloaded function type>’ and ‘option’)
std::cout.operator<< <option::do_something_stupid>(f1);
另一方面,clang++ 会产生不同的错误消息:
lsfov.cc:20:1: error: 'operator<<' cannot be the name of a variable or data member
operator<< <option::do_something_stupid>(std::ostream& o, const foo& f)
^
lsfov.cc:20:11: error: expected ';' at end of declaration
operator<< <option::do_something_stupid>(std::ostream& o, const foo& f)
^
;
lsfov.cc:20:12: error: expected unqualified-id
operator<< <option::do_something_stupid>(std::ostream& o, const foo& f)
^
lsfov.cc:33:15: error: reference to non-static member function must be called
std::cout.operator<< <option::do_something_stupid>(f1);
~~~~~~~~~~^~~~~~~~~~
它继续列出标准库中"<<"的可能重载(如果我理解正确的话),例如:
/usr/bin/../lib/gcc/x86_64-redhat-linux/5.3.1/../../../../include/c++/5.3.1/ostream:108:7: note: possible target for call
operator<<(__ostream_type& (*__pf)(__ostream_type&))
^
/usr/bin/../lib/gcc/x86_64-redhat-linux/5.3.1/../../../../include/c++/5.3.1/ostream:117:7: note: possible target for call
operator<<(__ios_type& (*__pf)(__ios_type&))
^
这是怎么回事?这种操作员专业化是否可能/允许?如果是这样,打电话给接线员的正确方式是什么?还是叮当正确,定义不成熟?
我认为clang不喜欢与专业化相关的friend
声明。重新排序它们可以解决问题。
enum class option : char { normal, do_something_stupid };
// forward declare the class and operator
class foo;
template<option E = option::normal>
std::ostream& operator<<(std::ostream& o, const foo& f);
// the class with the declared friend operator
class foo
{
private:
int i;
public:
explicit foo(int a=0) : i(a) {};
template<option E>
friend std::ostream& operator<<(std::ostream& o, const foo& f);
};
// the operator implementations
template<option E>
std::ostream& operator<<(std::ostream& o, const foo& f)
{ o << f.i; return o; }
template<> std::ostream&
operator<< <option::do_something_stupid>(std::ostream& o, const foo& f)
{
o << f.i + 1000;
return o;
}
此外,main
中使用的operator<<
不是成员cout
,而是全局。
int main()
{
foo f1(1), f2(2);
std::cout << "nf1= " << f1;
std::cout << "nf2= ";
/* this triggers an error with g++ */
operator<< <option::do_something_stupid>(std::cout, f1);
std::cout << "n";
return 0;
}
G++ 也对上面的代码感到满意。
关于非推导上下文中的运算符的说明。我假设您在某种更大的项目中使用这里的代码,但是如果运算符与非推导参数一起使用,则在成员方法或自由函数中实现该功能通常更容易和更清晰(根据需要使用 friend
)。