C++0x

ムーブセマンティクス(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>…

gcc 4.5.3を使うまで:健忘録

はじめてgccをビルドったのでメモ。 未来の自分 or gccでC++0x使ってみたいだけの人用。 shに書いたらそのまま通るノリで手順書いてみる。 http://www29.atwiki.jp/akcnv/pages/28.html ありがたいこのページを参考にする感じ。 環境はUbuntu10.04 EeePC 100…

ローカルな型とか無名型をテンプレート引数として使う

ローカルな型とか無名型をテンプレート引数として使える。 ・・・オブジェクト生成時ってどうするんだろ? とりあえず動くもの。 ローカルな型 #include <vector> #include <iostream> using namespace std; int main(){ struct data{ int a,b,c; } object = {1,2,3}; vector<data> v</data></iostream></vector>…

前回のカスタムデリータ使うときの話で

http://d.hatena.ne.jp/gintenlabo/20110121/1295626957にstd::functionの型消しの影響で最適化かかりにくいし、実行効率・メモリ効率も悪いよね的な事が書かれていたので、これは実測して試してみるしかないネという事なのでした。 #include <iostream> #include <memory> #in</memory></iostream>…

lambda式をもっと刺激的に使う

面白い話を小耳に挟んだのでメモメモ #include <iostream> #include <vector> #include <functional> using namespace std; int main(){ vector<vector<int>> array; vector<int> tempA = {1,2,3,4,5,6,7,8,9,10}; array.push_back( tempA ); vector<int> tempB = {11,12,13,14,15,16,17,18,19,20}; array.push_ba</int></int></vector<int></functional></vector></iostream>…

boost::threadとstd:functionとか

#include <iostream> #include <boost/thread.hpp> #include <Windows.h> using namespace std; int main() { // Aスレッド起動 boost::thread thr( [](){ // Aスレッド内でBスレッド起動 boost::thread thr( [](){ for(size_t i=0;i < 100; ++i){ cout << "B"; Sleep(100); } } ); for(size_t i=0;i </windows.h></boost/thread.hpp></iostream>…

イベントモドキ

std::list<std::function<bool ()>> ev_list; // 終了条件を満たしたとき、trueを返す関数オブジェクト auto lambda = []()->bool { static int i=0; cout << i++ << endl; if(i <= 20) return false; else return true; }; // リストの中にブチ込む ev_list.push_back(lambda); // </std::function<bool>…

lambdaさんの日常

#include <iostream> using namespace std; int main(){ auto lambda = []()->bool { static int i=0; cout << i++ << endl; if(i <= 20) return true; else return false; }; while( lambda() ); } 出力結果 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 便</iostream>…

trailing return type

#include <iostream> using namespace std; auto func()->int{ return 3; } int main(){ cout << func(); } 出力結果 3 ・・・オイコラ。絶対コレ、ラムダ式のついでに実装しただけだろ。 返り値の型を可変にしたいならtemplateでいいし、そもそもコレじゃ実現できない</iostream>…

std::random

Boost.randomのメルセンヌツイスター(あんまし偏らない乱数)がstd::tr1に入っていたので使ってみた。 #include <functional> #include <iostream> #include <algorithm> #include <random> #include <time.h> using namespace std; int main(){ // 現在時刻をシードにする mt19937 engine(static_cast<unsigned long>(time(0</unsigned></time.h></random></algorithm></iostream></functional>…

std::bindをやっと理解

bindが今までイマイチ理解できてなかったけど、書いてみたら一発で理解。 要は、function(関数ポインタ/関数オブジェクト/ラムダ式)の引数を束縛して新しいfunctionオブジェクトを生成してるのね。 #include <functional> #include <iostream> using namespace std; void Func(int </iostream></functional>…

std::bindを理解の続き

#include <functional> #include <iostream> using namespace std; struct Test{ void Func(int a, int b) { cout << a*b << endl; } }; int main(){ function<void (int)> func; Test t; func = bind(&Test::Func,&t,placeholders::_1,3); func(3); return 0; } func = bind(&Test::Func,&t,pl</void></iostream></functional>…

std::function

#include <functional> #include <iostream> using namespace std; // 関数オブジェクトを定義 class Func{ public: int operator()() { cout << "class str!" << endl; return 1; } }; // 関数を定義 int str() { cout << "func str!" << endl; return 2; } int main(){ function<int ()> </int></iostream></functional>…

std::unipue_ptrとstd::listその他の華麗な連携

VS2010すげえ! っていうか、C++0xが素敵っ! 下のようなのが書ける。 #include <list> #include <memory> #include <iostream> #include <utility> #include <algorithm> using namespace std; class Unit{ public: int pos_x,pos_y; Unit(int x, int y) : pos_x(x),pos_y(y){} virtual void Draw() = 0;</algorithm></utility></iostream></memory></list>…