我写的代码类似于:
std::string foo(bool b, const std::string& fst, std::string&& snd) {
return b ? fst : std::move(snd);
}
Run Code Online (Sandbox Code Playgroud)
把它扯snd出来并抄gcc出来.我试图最小化这个例子,我想出了:
#include <iostream>
#include <utility>
struct printer {
printer() { }
printer(const printer&) { std::cout << "copy" << std::endl; }
printer(printer&&) { std::cout << "move" << std::endl; }
printer(const printer&&) { std::cout << "const rvalue ref" << std::endl; }
};
int main() {
const printer fst;
printer snd;
false ? fst : std::move(snd);
}
Run Code Online (Sandbox Code Playgroud)
gcc 5.2输出
move
Run Code Online (Sandbox Code Playgroud)
clang 3.6输出 …
我想使用 SSE 实现以下功能。它将 a 中的元素与 b 中的压缩元素混合在一起,其中元素仅在使用时才存在。
void packedBlend16(uint8_t mask, uint16_t* dst, uint16_t const* a, uint16_t const* b) {
for (int i = 0; b < 8; ++i) {
int const control = mask & (1 << i);
dst[i] = control ? a[i] : *b++;
}
}
Run Code Online (Sandbox Code Playgroud)
对我来说棘手的部分是在向量中正确地间隔 b 的元素。
到目前为止,我的方法是:
LUT[mask]以进行随机播放,将 b 的元素扩展到正确的位置pshufbvpbroadcastw+ vpand+从蒙版构造混合向量vpcmpeqwpblendvb a 与洗牌的元素 b我怀疑 256 个条目的 LUT 不是最好的方法。我可能有 2 16 个入口 LUT 用于高位/低位。但是您必须根据掩码的低 4 位的 …
我正在看标准的5.16第3段,试图了解发生了什么.考虑M定义为的类型
struct M {
M();
M(const M&);
M(M&&);
};
Run Code Online (Sandbox Code Playgroud)
如果我有一个三元表达pred ? E1 : E2,其中的类型E1IS const M&和类型E2就是M&&确实5.16第3段子弹1适用?
- 如果E2是左值:如果E1可以被隐式转换(第4节)到类型"左值引用T2",则E1可以被转换为匹配E2,受制于转换中引用必须直接绑定的约束(8.5. 3)到左值.
我认为它没有,因为要进行隐式转换const M&,这需要M具有成员函数operator const M&().但是,我不确定,因为它可以const M隐式转换,是否可以隐式添加引用?
如果它是隐式可转换的,M&&直接绑定到const M&?我经历了8.5.3中的程序,我认为第5段子弹2是这个案例所在的地方,所以它确实直接绑定,但我不确定.
- 如果初始化表达式[..]具有类类型(即,T2是类类型),其中T1与T2不是引用相关的,并且可以隐式转换为xvalue,类prvalue或类型的函数lvalue "cv3 T3",其中"cv1 T1"与"cv3 T3"参考兼容