C++11

move semanticsについて

人/後輩に教える時用の脳内カンペ。ムーブセマンティクス(move semantics)というのは、コピーではなく、所有権の移動をさせるように動作するような考え方のこと。 例えば、下記のコードで MyClass a; // aというインスタンスを作成 MyClass b = a; // aの中…

暗黙のmoveとNRVO

VS2010(VC10)にて関数で返す値についての扱いをついったーで突っ込まれて実際書いて確かめた時のメモ。 #include <iostream> #include <vector> #include <boost/timer.hpp> struct Test{ std::vector<int> tmp; Test(){ std::cout << "コンストラクタ" << std::endl; } ~Test(){ std::cout << "デス</int></boost/timer.hpp></vector></iostream>…

range-based for

gcc 4.6.1をビルドしたので動作確認。 #include <iostream> #include <list> #include <memory> struct Task{ int id; Task(int id) : id(id){ std::cout << "Create Task : " << id << std::endl; } ~Task(){ std::cout << "Delete Task : " << id << std::endl; } void update(){ s</memory></list></iostream>…

ムーブセマンティクス(Move Semantics)はテクニックにすぎない

正確には、C++0xではムーブセマンティクスを実現"しやすく"なっただけで、 ムーブ自体は昔からのテクニックに過ぎない。 という事をやっと理解したっぽい?メモ。 #include <iostream> #include <vector> #include <string> std::vector<std::string> add_bar(std::vector<std::string> lhs) { lhs.push_back("bar</std::string></std::string></string></vector></iostream>…