move

Why arguments moved twice when constructing std::thread

对着背影说爱祢 提交于 2020-03-10 04:14:26
问题 Consider this program that essentially creates std::thread that calls the function func() with arg as argument: #include <thread> #include <iostream> struct foo { foo() = default; foo(const foo&) { std::cout << "copy ctor" << std::endl; } foo(foo&&) noexcept { std::cout << "move ctor" << std::endl; } }; void func(foo){} int main() { foo arg; std::thread th(func, arg); th.join(); } My output is copy ctor move ctor move ctor As far as I understand arg is copied internally in the thread object

十二、 C++特性之 杂合

一笑奈何 提交于 2020-03-06 23:50:02
static_assert和 type traits static_assert提供一个编译时的断言检查。如果断言为真,什么也不会发生。如果断言为假,编译器会打印一个特殊的错误信息。 template < typename T, size_t Size> class Vector { static_assert(Size < 3, "Size is too small"); T _points[Size]; }; int main() { Vector< int, 16> a1; Vector< double, 2> a2; return 0; } error C2338: Size is too small see reference to class template instantiation 'Vector<T,Size>' being compiled with [ T= double, Size=2 ] static_assert和type traits一起使用能发挥更大的威力。type traits是一些class,在编译时提供关于类型的信息。在头文件<type_traits>中可以找到它们。这个头文件中有好几种 class: helper class,用来产生编译时常量。type traits class,用来在编译时获取类型信息,还有就是type

Should the Copy trait always be implemented if possible?

前提是你 提交于 2020-03-01 01:50:07
问题 You can implement the Copy trait to give the type copy-semantics instead of move-semantics. This can only be done if all its constituent elements (each factor of a product type, or each factor of each variant of a sum-type) are also Copy . This allows you to also make rather large types Copy . Can implementing Copy be detrimental to performance if the size of the type is "large"? If Copy should always be implemented, why is it not an auto-trait like Sync and Send for those types which can

C++11新特性:std::move()和std::forward()

只谈情不闲聊 提交于 2020-02-29 05:51:14
C++11通过std::move()和std::forward()函数分别实现了左值转右值和完美转发的功能。 对于std::move(),考虑如下情形: void func(int &&args) { std::cout << args << std::endl; } int a = 10; func(20); // ok func(a); // error, 右值引用不能绑定左值 func(std::move(a)); // ok 对于std::forward(),完美转发,当 void func(int &arg) { std::cout << "func lvalue" << std::endl; } void func(int &&arg) { std::cout << "func rvalue" << std::endl; } template <typename T> void wrapper(T &&args) { func(args); } int main() { int a = 10; wrapper(a); wrapper(20); return 0; } 以上函数输出: func lvalue func lvalue 虽然我们调用wrapper()函数时,传入的参数一个是左值,一个是右值,但是最终的输出确都是左值

HTML5 Limit Moving Image Into Canvas

别说谁变了你拦得住时间么 提交于 2020-02-25 08:43:11
问题 canvas.onmousemove = function (e) { if (isDown === true) { var pos = getMousePos(canvas, e); var x = pos.x; var y = pos.y; //translate difference from now and start element.translate(x - startX, y - startY); drawImage(); //update start positions for next loop startX = x; startY = y; }} http://jsfiddle.net/braziel/nWyDE/ Friends, I need help to limit the movement of an image within the canvas when zoomed. I put the link, you can see the image when zoomed and dragged it exceeded the limit of

HTML5 Limit Moving Image Into Canvas

我是研究僧i 提交于 2020-02-25 08:43:11
问题 canvas.onmousemove = function (e) { if (isDown === true) { var pos = getMousePos(canvas, e); var x = pos.x; var y = pos.y; //translate difference from now and start element.translate(x - startX, y - startY); drawImage(); //update start positions for next loop startX = x; startY = y; }} http://jsfiddle.net/braziel/nWyDE/ Friends, I need help to limit the movement of an image within the canvas when zoomed. I put the link, you can see the image when zoomed and dragged it exceeded the limit of

js坦克大战

▼魔方 西西 提交于 2020-02-22 05:06:40
bug一大堆 代码下载 <!DOCTYPE html> <html> <head> <title>tank</title> <style type="text/css"> body { margin: 0px; padding: 0px; border: 0px; } .map { position: absolute; top: 30px; width: 390px; height: 390px; left: 50%; margin-left: -200px; border: 9px solid orange; background-color: #8B8989; } .mapchild { position: absolute; background-size: cover; } #ifo { position: absolute; top: 30px; width: 418px; height: 418px; left: 50%; margin-left: -200px; color: green; text-align: center; background-color: #FAEBD7; z-index: 10; } </style> </head> <body> <div id="ifo"> <h1 id="ifo_title"></h1> <h3>按键说明:</h3>

Moving a lambda: once you've move-captured a move-only type, how can the lambda be used? [duplicate]

徘徊边缘 提交于 2020-02-12 11:53:16
问题 This question already has answers here : How to create an std::function from a move-capturing lambda expression? (3 answers) Closed 4 years ago . This answer explains how to move-capture a variable within a lambda in C++14. But once you've move-captured an un-copyable object (such as a std::unique_ptr ) within a lambda, you cannot copy the lambda itself. This would be fine if you could move the lambda, but I get a compile error when trying to do so: using namespace std; class HasCallback {

制作屏保,图片的移动

牧云@^-^@ 提交于 2020-02-03 15:01:16
public partial class ImageMove : Form { public ImageMove() { InitializeComponent(); } int x = 1;//x为1代表水平向右移动,-1代表水平向左移动 int y = 1;//y为1代表垂直向下移动,-1代表垂直向上移动 int move = 5;//每次移动的距离,水平和垂直都是一样的 private void timer1_Tick(object sender, EventArgs e) { //根据x值设置图片的位置 if (x == 1) { this.pictureBox1.Left += move; } else { this.pictureBox1.Left -= move; } //根据y值设置图片位置 if (y == 1) { this.pictureBox1.Top += move; } else { this.pictureBox1.Top -= move; } //水平方向 //撞到右边(减20主要是考虑到窗体有边框) if(x==1&&this.pictureBox1.Left+this.pictureBox1.Width>=this.Width-20) { x = -1; } //撞到左边 else if (x == -1 && this.pictureBox1

C++ Debug:Invalid address specified to RtlValidateHeap

一世执手 提交于 2020-02-02 15:39:39
@Author:CSU张扬 @Email:csuzhangyang@gmail.com or csuzhangyang@qq.com @我的网站: https://www.faker.top Invalid address specified to RtlValidateHeap 1. 问题概述 报错如下: HEAP [ String . exe ] : Invalid address specified to RtlValidateHeap ( 02730000 , 0274 ED98 ) 监视窗口各变量的值: 异常位置发生在 v.push_back(std::move(s2)); ,该语句进入了 free() 函数, 异常最后在函数 free() 的最后。 free() 是我自定义的 String 类的析构函数内容,释放 allocator 分配的内存。 class String { public : // 省略一部分代码。。。 // 正确的代码 String ( String && s ) noexcept : sz ( s . sz ) , p ( s . p ) { s . p = 0 ; s . sz = 0 ; std :: cout << "move constructor" << std :: endl ; } // 错误的代码 // String(String&