2009-01-01から1年間の記事一覧

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>…

渡されたブロックをProcで取得する

忘れがちなのでメモ。 rb_block_proc();

rb_iterate

忘れがちなのでメモ。 #include "ruby.h" static VALUE main_proc(VALUE arg) { VALUE each = arg; return rb_funcall(each, rb_intern("call"), 0); } static VALUE block(VALUE call_arg, VALUE block_arg, VALUE self) { rb_p(call_arg); rb_p(block_arg)…

win32fiber(失敗)

WindowsのFiberでcoroutineを作れないかと思ったものの、案の定、失敗。 動かないソースだけ貼り付けとく。 #include <windows.h> #include "ruby.h" static VALUE Fiber; static VALUE FiberError; struct win32fiber { LPVOID main_fiber; LPVOID fiber; VALUE proc; </windows.h>…

Queueによるcoroutineもどき

require 'thread' class Coroutine def initialize(&block) raise 'block not given' unless block_given? @q = Queue.new @th = Thread.start do @q.pop block.call(self) end end def yield @q.pop end def resume(*args) @q.push(args) end end coroutine…

インディ大高ジョーンズ

なつかしーwww

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>…

メモだけ

C++

#include <iostream> using namespace std; template <int N, int _3 = N % 3, int _5 = N % 5> struct FizzBuzz { FizzBuzz() { FizzBuzz<N - 1>(); cout << N << " "; } }; template <int N, int _5> struct FizzBuzz<N, 0, _5> { FizzBuzz() { FizzBuzz<N - 1>(); cout << "Fizz "; } }; template <int N, int _3> struc…</int></n></n,></int></n></int></iostream>

gflags

C++

http://storehouse.sakura.ne.jp/viewvc/viewvc.cgi/gflags/?root=svn 一応、VCでもビルドできた。 #include <iostream> #include "gflags/gflags.h" #include "gflags/gflags_completions.h" DEFINE_bool(big_menu, false, "Include 'advanced' options in the menu l</iostream>…

cpp圧勝

C++

http://q.hatena.ne.jp/1243223376 VCが多いせいかも。その他が気になる。

vectorの二次元配列

C++

便利なのか微妙なところだ…。 #include <iostream> #include <vector> using namespace std; void f(vector< vector<int> >& vv_) { vector< vector<int> > vv = vv_; for (vector< vector<int> >::const_iterator i = vv.begin(); i != vv.end(); i++) { vector<int> v = *i; for (vector<int>::const_i</int></int></int></int></int></vector></iostream>…

link:typedefテンプレート

C++

http://msdn.microsoft.com/ja-jp/library/cc440199(VS.71).aspx ふむふむ。

typedefテンプレートもどき

C++

typedefにテンプレートは使えないので、継承で何とかしてみる。 #include <iostream> #include <vector> using namespace std; template<typename T> class vecvec : public vector< vector<T> > {}; template <class T> void func(vecvec<T> vv) { for (vecvec<T>::const_iterator i = vv.begin() ; i != vv.</t></t></class></t></typename></vector></iostream>…

typedefテンプレートもどき その2

C++

structを使ってみる。 #include <iostream> #include <vector> using namespace std; template<class T> struct vecvec { typedef vector< vector<T> > type; }; void func(vecvec<int>::type& vv) { for (vecvec<int>::type::const_iterator i = vv.begin() ; i != vv.end() ; i++) { for (vector<int>::c</int></int></int></t></class></vector></iostream>…