C++

Boost.AsioでS3からファイルを取得

表題の通り。 CentOSのBoostのバージョン、上がらないかな…。 #include <iostream> #include <istream> #include <ostream> #include <string> #include <boost/asio.hpp> using boost::asio::ip::tcp; int main(int argc, char* argv[]) { try { std::string host = argv[1]; std::string path = argv[2]; std::s</boost/asio.hpp></string></ostream></istream></iostream>…

しつこくラムダ式を使う

C++

↓のようなことしようとした。 function make_counter(n) { f = function() { return n++; }; g = function() { return n++; } } make_counter(100); print(f()); // 100 print(g()); // 101 ローカル変数を参照でキャプチャするのはさすがに不味そうなので、…

ラムダ式を使う

C++

autoも普通に使えるし、C++を書いている感じじゃなかった。 #include <iostream> #include <functional> using namespace std; int main() { auto make_counter = [](int n) -> std::function<int ()> { return [=]() -> int { // コピーによるキャプチャだとconstになるらしい int &i = co</int></functional></iostream>…

ラムダ式を使う: フィボナッチ数

C++

fibがautoはさすがにダメだった。当たり前のような気もする。 あと参照によるキャプチャじゃないとSEGV。 #include <iostream> #include <functional> using namespace std; int main() { function<int (int)> fib = [&](int n) -> int { if (n < 2) { return n; } else { return fib(n - 2) +</int></functional></iostream>…

g++ 4.1.3でbayonがビルド出来なかった件

C++

結論としてはg++ 4.2.4にアップデートしたらビルドできた。どうもgoogle sparsehashのhash関数が、__gnu_cxx::hashだとダメっぽい(std::stringをハンドルするテンプレートがないみたいだった) g++ 4.2.4でstd::tr1::hashを使うようになったらビルドが通っ…

右辺値参照が分からない

C++

「一時オブジェクトを渡したときに移動できるようにするのね」と理解したつもりでコードを書いてみたけど、よく分からないところが… #include <string> using namespace std; class Foo { string s; public: Foo(const char* cs) { cout << "constructor" << endl; }</string>…

Flareのクライアントを書こうと思ったんだけど…

C++

SEGV。使い方がよく分からないなー。 #include <iostream> #include "client.h" using namespace std; using namespace gree::flare; int main() { client c("localhost", 12121); storage::entry e; cout << c.connect() << endl; c.get("key1", e); c.disconnect(); </iostream>…

コードの圧縮を試みる

C++だと似たようなコードを繰り返し書いていたので、コードの圧縮を考えてみる。 #include <string> #include <map> #include "hello.h" using std::string; using std::map; class MyMap : public DataWrapper<MyMap> { std::map<string, string> m; DEF(initialize, (0, ())) { return Qnil; } </string,></mymap></map></string>…

bayon: VCでビルドできた

C++

http://storehouse.sakura.ne.jp/viewvc/viewvc.cgi/test-bayon/?root=svn 現状ではSTLのMapではビルドできないみたい。VCだからかなぁ。

bayonのRubyバインディング

http://storehouse.sakura.ne.jp/viewvc/viewvc.cgi/ruby-bayon/?root=svn http://storehouse.sakura.ne.jp/viewvc/viewvc.cgi/ruby-bayon/ext/bayon.cpp?root=svn&view=markup おおむね出来た。 require 'bayon' include Bayon $users = { 1 => '阿佐田', 2…

SLTのMapにresize()はない?

C++

bayonをビルドしようとして引っかかった。 VCとgccで確認。

2進数表示

C++

JavaのInteger#toString()みたいに、基数を指定して文字列に変換ってないのかなー。 #include <iostream> template<class T> void println_binary(T x) { static const char * const table[] = { "0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "100</class></iostream>…

staticメンバなvectorの初期化

C++

間違ってないかなぁ… #include <iostream> #include <vector> using namespace std; struct Foo { static const vector<int> v; static vector<int> new_v(); }; vector<int> Foo::new_v() { vector<int> v; v.push_back(1); v.push_back(2); v.push_back(3); return v; } const vector<int> Foo::v = Fo</int></int></int></int></int></vector></iostream>…

staticメンバなvectorの初期化 その2

C++

普通に使える感じだけど、罠がある気が。 #include <iostream> #include <vector> using namespace std; class Foo { static const vector<int> v; static const vector<int> new_v(); public: void func() { for(vector<int>::const_iterator i = Foo::v.begin(); i != Foo::v.end(); i++) { </int></int></int></vector></iostream>…

サイズの違うvectorの代入

C++

大きい場合 #include <iostream> #include <vector> int main() { using namespace std; vector<int> v(3); for (vector<int>::iterator i = v.begin(); i != v.end(); i++) { cout << *i << endl; } v = vector<int>(5, 1); for (vector<int>::iterator i = v.begin(); i != v.end(); i++) { cout <</int></int></int></int></vector></iostream>…

TR1: shared_ptr、真偽値テスト

C++

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1836.pdf 「2.2.3.5 shared_ptr observers」に「operator unspecified-bool-type () const;」の定義があった。 operator unspecified-bool-type () const; 16 Returns: an unspecified value that…

shared_ptr、bool、キャスト

C++

Boostだとboolへのキャストはオーバーロードされてた。 // implicit conversion to "bool" #if ( defined(__SUNPRO_CC) && BOOST_WORKAROUND(__SUNPRO_CC, < 0x570) ) || defined(__CINT__) operator bool () const { return px != 0; } 追記 その下に#elif…

null値のshared_ptr

C++

melponnさんにコメントをいただいたので早速使ってみた。 #include <iostream> #include <string> #include <vector> #include <memory> using namespace std; using namespace std::tr1; int main() { typedef shared_ptr<string> strp; vector<strp> v; v.push_back(strp(new string("foo"))); v.push_back(</strp></string></memory></vector></string></iostream>…

null値のshared_ptr その2

C++

http://kabuki.spring8.or.jp/Members/matusita/forums/cpp-tips/document.2005-10-09.6758608810 「shared_ptr(static_cast(0))」じゃなくて、デフォルトコンストラクタで生成すれば良かったのか・・・。 「if (p)」は間違い? 追記 ひょっとして「shared_ptr:…

scoped_ptrでメンバ変数

C++

なんかもう「Boost使っちゃいYo!」って気がしてきた。 #include <iostream> #include <string> #include <boost/scoped_ptr.hpp> using namespace std; class Child { public: Child() { cout << "Child::new(): " << this << endl; } ~Child() { cout << "Child#delete(): " << this << endl; } }; c</boost/scoped_ptr.hpp></string></iostream>…

_Bool_type

C++

_Bool_typeが気になったので少し調査。 #include <iostream> using namespace std; class TrueFalse { bool r_; public: TrueFalse(bool r) : r_(r) {}; operator std::_Bool_type() const { return r_ ? _CONVERTIBLE_TO_TRUE : 0; } }; void print_bool(TrueFalse& t</iostream>…

vectorの2次元配列をポインタにする

C++

正しいんだろうか…? #include <iostream> #include <string> #include <vector> using namespace std; int main() { vector< vector<int>* >* pvv; pvv = new vector< vector<int>* >(); pvv->push_back(new vector<int>(4)); pvv->push_back(new vector<int>(4)); pvv->push_back(new vector<int>(4)); /* ↓だ</int></int></int></int></int></vector></string></iostream>…

vector、null、ポインタ

C++

JavaのListみたな感覚でvectorを使おうとすると、要素をポインタにする必要が出てくる。 ポインタ使わずにnull値を表現するうまい方法はないかなぁ…

Boostでありがちなこと

C++

リンクするライブラリが分からなくなる。

Cygwinでasioが動かない

C++

無言で死ぬ。なーぜーだー #define _WIN32_WINNT 0x0501 #define __USE_W32_SOCKETS #include <iostream> #include <string> #include <boost/asio.hpp> using namespace std; using namespace boost::asio; int main() { ip::tcp::iostream s("www.example.com", "http"); s << "GET / HTTP/1.</boost/asio.hpp></string></iostream>…

C++ Coding Standards

C++

C++ Coding Standards―101のルール、ガイドライン、ベストプラクティス (C++ in‐depth series)作者: ハーブサッター,アンドレイアレキサンドレスク,浜田光之,Herb Sutter,Andrei Alexandrescu,浜田真理出版社/メーカー: ピアソンエデュケーション発売日: 200…

適当にauto_ptrを実装する

C++

#include <iostream> #include <string> using namespace std; template<class T> class AutoPtr { T *p_; public: AutoPtr(T *p) { cout << "new:" << this << endl; p_ = p; } ~AutoPtr() { cout << "delete:" << this << endl; if (p_) { cout << "!!delete object!!" << endl; delet</class></string></iostream>…

cygwin, gcc4, tr1

C++

このソースが一応、cygwinでもビルドできた。 #include <iostream> #include <tr1/functional> using namespace std; int count1(int n) { return ++n; } int count2(int &n) { return ++n; } int count3(int *n) { return ++(*n); } int main() { int n1 = 0, n2 = 0, n3 = 0; tr1::fu</tr1/functional></iostream>…

静的なメンバ変数の初期化

C++

多分、問題ない…と思う。 class Foo { static const int bar[]; }; const int Foo::bar[] = {1, 2, 3}; int main() { return 0; } 実態の定義にstaticをつけるとエラー。 class/structで使う修飾子だから?

placement new

C++

使う機会はなさそうな。 #include <iostream> #include <new> using namespace std; class Foo { int i_; public: Foo() : i_(0) { cout << "Foo(0)" << endl; } Foo(int i) : i_(i) { cout << "Foo(" << i_ << ")" << endl; } Foo(const Foo& foo) { cout << "copy constru</new></iostream>…