模板化重载操作符的显式调用
explicit call of templated overloaded operator
我有一个重载操作符的类。
class sout {
public:
template<typename T>
friend inline sout& operator&(sout&, T&);
friend inline sout& operator&(sout&, std::string&);
};
现在如果我使用模板化操作符&在string .operator&出现错误:
代码:sout& operator&(sout& s, std::string& str) {
uint16_t ts = static_cast<uint16_t>(str.size()); // this is ok
s & ts; // is also ok
s & static_cast<uint16_t>(str.size()); // but this is wrong
// ...
return s;
}
错误:Error:C2679: binary '&' : no operator found which takes a right-hand operand of type 'uint16_t' (or there is no acceptable conversion)
could be 'sout &operator &<uint16_t>(sout &,T &)'
with
[
T=uint16_t
]
or 'sout &operator &(sout &,std::string &)'
while trying to match the argument list '(sout, uint16_t)'
,然后尝试使用显式运算符&模板类型:
operator&<uint16_t>(s, ts); // this also ig ok
但是如果我把它们组合起来,我又会报错:
operator&<uint16_t>(s, static_cast<uint16_t>(str.size())
错误:'operator &' : cannot convert parameter 2 from 'uint16_t' to 'uint16_t &'
i also try reinterpret_cast.
i know operator&正在期待对uint16_t的引用,而size()函数返回size_t (int)而不是引用。有可能在一行中完成吗?
问题是size()
返回的值是一个临时值,临时值是右值;然而,你的函数接受一个左值引用。下面的代码片段澄清了这个问题:
int foo() { return 42; }
void bar(int& i) { i++; } // Parameter is an lvalue reference to non-const
int main()
{
bar(foo()); // ERROR! Passing an rvalue to bar()
bar(1729); // ERROR! Passing an rvalue to bar()
int i = 42;
bar(i); // OK! Passing an lvalue to bar()
}
左值引用不能绑定到右值,除非它们是对const
的引用。
template<typename T>
friend inline sout& operator&(sout&, T const&);
// ^^^^^
如果你的operator&
应该修改右手参数,所以引用不能是对const
的引用,在c++ 11中你可以使用右值引用(这将允许绑定到左值,以及由于c++ 11的引用崩溃规则):
template<typename T>
friend inline sout& operator&(sout&, T&&);
// ^^^
相关文章:
- boost::make_shared没有调用(放置)操作符new
- 通过调用Move赋值操作符实现Move构造函数
- 如何从复制赋值操作符调用复制构造函数
- 我如何使用“函数调用操作符”来加载“右值”类型到我的对象
- 多个操作符重载会导致歧义调用
- 不同的强制转换操作符被不同的编译器调用
- c++中的操作符重载,无法调用双参数构造函数
- 即使类没有定义构造函数,也不能在类上调用c++默认的move赋值操作符
- 有没有一种方法可以通过函数指针来调用操作符函数?
- 程序没有调用正确的重载操作符
- 调用转换操作符不能用于静态const变量
- 使用:TYPENAME没有提供调用操作符,从pair获取值失败
- 是否不可能手动调用c++操作符?
- 重载函数调用操作符和赋值
- 从另一个操作符重载函数调用操作符重载函数:性能
- 编译器如何处理函数函数中重载的函数调用操作符
- 如何通过模板调用操作符重载
- 为什么构造函数不调用函数调用操作符
- <在map中使用struct作为键时未调用操作符
- Const调用操作符调用绑定的非Const成员函数