- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
làm mới:澄清、更明确的重点和缩短的示例:
M op+(M&&,M&&)
过载吗?假设,我想很好地处理 RValues?我想其他三个重载Đúng必需的。我首先使用 (&&,&&)
重载的原因:
M op+(&&,&&)
,但我似乎需要它:当为(&&,&)
Và提供重载时(&,&&)
编译器陷入歧义。有没有比添加另一个实现变体更好的解决方法?您还可以查看 complete代码。
struct Matrix {
...
// 2ary ops
friend Matrix operator+(const Matrix &a, Matrix &&b ) { b+=a; return move(b); }
friend Matrix operator+(Matrix &&a, const Matrix &b) { a+=b; return move(a); }
friend Matrix operator+(const Matrix &a, Matrix v) { v+=a; return v; }
friend Matrix operator+(Matrix &&a, Matrix &&b) { a+=b; return move(a); }
// ... same for operator*
// ... assume impl of operator+=,*= and move semantics
};
int chính() {
Matrix a{2},b{3},c{4},d{5};
Matrix x = a*b + c*d; // reuires &&,&& overload
std::cout << x << std::endl;
}
1 Câu trả lời
如果第一个值是右值,则以下辅助函数返回第一个值,否则返回第二个值(可能是右值,但也可能不是)。
template
typename std::enable_if::value, T1&&>::type
get_rvalue(T1&& t1, T2&& t2) { return std::forward(t1); }
template
typename std::enable_if<>::value, T2&&>::type
get_rvalue(T1&& t1, T2&& t2) { return std::forward(t2); }
下面的辅助函数返回上面没有返回的其他值。
template
typename std::enable_if::value, T1&&>::type
get_non_rvalue(T1&& t1, T2&& t2) { return std::forward(t2); }
template
typename std::enable_if<>::value, T2&&>::type
get_non_rvalue(T1&& t1, T2&& t2) { return std::forward(t1); }
这只是比较两种类型是否相同,忽略引用和常量。
template
struct is_same_decay : public std::is_same<
typename std::decay::type,
typename std::decay::type
> {};
然后我们可以像下面这样(使用模板)为每个函数做一个重载:
// 2ary ops
template
friend typename std::enable_if<
is_same_decay::value &&
is_same_decay::value,
Matrix>::type
operator+(M1&& a, M2&& b)
{
Matrix x = get_rvalue(std::forward(a), std::forward(b));
x += get_non_rvalue(std::forward(a), std::forward(b));
return x;
}
template
friend typename std::enable_if<
is_same_decay::value &&
is_same_decay::value,
Matrix>::type
operator*(M1&& a, M2&& b)
{
Matrix x = get_rvalue(std::forward(a), std::forward(b));
x *= get_non_rvalue(std::forward(a), std::forward(b));
return x;
}
注意上面,如果 M1
hoặc M2
是一个右值,get_rvalue(a, b)
将返回一个右值,因此在这个case Matrix x
将由 move 而不是副本填充。命名返回值优化可能会确保不需要复制(甚至 move )到返回值中,因为 x
将在返回值的位置构造。
完整代码是 đây .
关于operator-overloading - C++11 重载 `M operator+(M&&,M&&)`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7173498/
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎离题,因为它缺乏足够的信息来诊断问题。 更详细地描述您的问题或include a minim
家庭作业 必须同时重载 operator>(istream &in, Complex &value); public: // constructor Compl
这个问题在这里已经有了答案: What are the basic rules and idioms for operator overloading? (8 个答案) 关闭 5 年前。 有什么区别
这是文档中一个不清楚的示例,使用此运算符:http://package.elm-lang.org/packages/elm-lang/core/3.0.0/Json-Decode#at 最佳答案 请注
我不明白这种行为: > sort([1,2,3,4]) ~~ sort([1,2,3,4]) False 你能给我解释一下吗? 为什么这两个列表(显然是相等的)根据 Perl 6 不相等。 更新 有趣
我正在尝试将 Ø 设为逻辑否定运算符。 ¬ True; multi sub prefix: ($n) { return not $n; } 当我运行上面的程序时,它返回以下错误: $
class Port { private: char * brand; char style[20]; // i.e., tawny, ruby, vintage int bo
早上好。我有一些问题。我有这些字段: name: "Mike", city: "NY", address: "something", pets: ["dog", "cat"] 我创建了索引 db.pe
我有以下代码使用 Javascript Webcrypto-API 解密 AES 加密数据,但它会导致“OperationError”并显示消息“操作因操作特定原因而失败”: function loa
我制作了一个自定义 Airflow 操作符,这个操作符接受一个输入,这个操作符的输出在 XCOM 上。 我想要实现的是使用一些定义的输入调用运算符,将输出解析为可在分支运算符内部调用的 Python,
int a = 1; a += ++a; cout << a << endl; // 4 int a = 1; a += a++; cout << a << endl; // 3 为什么这两个例子有不
我有这个队列的实现: #include using namespace std; template struct elem_q { T inf; elem_q* link; }; template
Thật khó để biết nên hỏi gì ở đây. Câu hỏi này mơ hồ, không đầy đủ, quá rộng hoặc mang tính tu từ và không thể trả lời hợp lý theo hình thức hiện tại. Để được trợ giúp làm rõ vấn đề này để có thể mở lại, hãy truy cập trung tâm trợ giúp. Đóng 1
我对 C++ 中的运算符重载有疑问。我有代表复数的结构,我正在重载运算符,所以我可以用复数进行计算。Visual Studio 2012 给我这个错误:1 IntelliSense:没有运算符“!=”
我正在尝试创建一个 BoolArray 类,它表示一个 bool 值数组,而不是为每个 bool 变量保存 1 个 bool 值。它使用 1 个字符变量来表示使用位的 8 个 bool 值,并节省内存
我类有 2 个运算符有点问题。 我的类(class)宣布: template class MyMap{ keyType keys[MAX]; valueType values[MAX
我有类的迭代器模板和用于 for 语句的类。 template class Itr2 { public: Itr2() { } ~Itr2() { } typedef t
Section 7.2 Enumeration declarations 没有说明任何关于 operator!=() 和作用域的 operator==()枚举。但是下面的代码可以编译。 #includ
我需要将一个 int 序列化到本地文件并将其读入内存。这是代码 #include "stdafx.h" #include #include using namespace std; int _tm
operator += 这样定义对吗?! void operator +=(const BigNumber& other) { *this=(*this) + other; } 在这样的类中:
Tôi là một lập trình viên xuất sắc, rất giỏi!