c++

initializer_listもどきをc++03で実装してみる

c++

c++0xにはinitializer_listというPOD構造体や配列の初期化に使うような記述、 int ar[] = { 1, 2, 3, 4, 5, 6 }; と言うような表記で初期化することができます。 ですが、同じく配列のようにつかうstd::vectorなどはそういう初期化が出来ません。 現在策定中…

functor版tolower

c++

#include <locale> #include <iostream> #include <string> #include <functional> #include <algorithm> int main() { std::locale loc("ja_JP.UTF-8"); const std::ctype<char>& cty = std::use_facet<std::ctype<char> >(loc); std::string str = "HELLO WORLD."; std::cout << str << std::endl; std::transform(str.be…</std::ctype<char></char></algorithm></functional></string></iostream></locale>

tolower

c++

#include <locale> #include <iostream> #include <string> int main() { std::locale loc("ja_JP.UTF-8"); const std::ctype<char>& cty = std::use_facet<std::ctype<char> >(loc); std::string str = "HELLO WORLD."; std::cout << str << std::endl; for (std::string::iterator itor = str.begin(); itor </std::ctype<char></char></string></iostream></locale>…

ICU charset detector

c++

ICU(International Components for Unicode)というライブラリのなかにcharset detectorという文字コード/文字種判定器がはいってます。使い方は下のような感じ #include <iostream> #include <fstream> #include <unicode/ucsdet.h> #include <string> int main(int argc, char** argv) { if (argc > 2) re</string></unicode/ucsdet.h></fstream></iostream>…

型配列

c++

複数の型をlistに束縛してnthで取り出してみる。 中身はlispちっくにconsセルでつなげてます。 #include <iostream> #include <typeinfo> struct nil_t {}; template <typename... args> struct cons_cell; template <typename first, typename... rest> struct cons_cell<first, rest...> { typedef first car; typedef cons_cell<rest...> cdr; }; template </rest...></first,></typename></typename...></typeinfo></iostream>

variadic template事始め

c++

C++0xでvariadic templateを使って可変数引数の数を数えるプログラムを組んでみる。 使ったのはg++ Ubuntu 4.3.2-1ubuntu12 オプションは g++ -g -Wall -std=c++0x -o variadic_templ variadic_templ.cpp #include <iostream> #include <typeinfo> template <typename... arg> struct length; temp</typename...></typeinfo></iostream>…

オブジェクトファイルの奇妙なcall命令

http://lucille.atso-net.jp/blog/?p=603 これなんですが、たしか参照解決前の関数エントリのインデックスだったような・・・ ということで確認してみる。まずはコンパイルの準備。 ヘッダファイル。 extern "C" void test_c_func(); void test_cpp_func(); …

コンパイル時リスト処理続き

c++

g++で通らなかったのは型指定してなかったからのようです。 template <typename T> struct generate_multiply_table { typedef typename map_list< bind_1st<multiply,T>::template currying_type, iota<5,0>::type>::type type; } このように書き換えたところちゃんとg++でもコンパ</multiply,t></typename>…

コンパイル時リスト処理

c++

前回の日記からコード整理してみました。 ソースはこちら前のコードだと二重mapが実現できなかった(というか実現するためにmapの中身をそのたびに書き換えないといけなかった)んですが、こちらの「テンプレート引数にクラステンプレートを渡す話」みてできそ…

テンプレートを使ったコンパイル時数値計算

c++

#include <iostream> #include <typeinfo> template <typename T, int length> struct array { typedef T element_type; element_type elements[length]; element_type& operator[](int offset) { return elements[offset]; } const element_type& operator[](int offset) const { return elements[offset</typename></typeinfo></iostream>…

バイトオーダ反転、ビットオーダ反転

c++

#include <iostream> template <int value_, typename T = int, int size = sizeof(T)> struct byte_order_reverser { enum { msb = (value_ >> ((size - 1) * 8)), other = (value_ - (msb << ((size - 1) * 8))), reverse_value = (byte_order_reverser<other, T, size-1>::reverse_value << 8) + byte_order_reverser</other,></int></iostream>

ファイル全体読み込み

c++

ファイルの中身を全部読み込むから。自分が書くとこういう感じですかね。 #include <iostream> #include <fstream> #include <iterator> #include <iomanip> #include <vector> int main() { std::ifstream ifs("file_read.cpp"); if (!ifs) { std::cerr << "file can not open." << std::endl; return -1; </vector></iomanip></iterator></fstream></iostream>…

コンパイル時cos()

C++

コンパイル時に計算してくれるcos()関数ってなんか面白そう?と思って作ってみました。 関数をテイラー展開してそれを演算してるだけです。 やー、lispと違って末尾再帰じゃなくても折りたたまれるのは楽でいいなぁ(笑) template <typename ValueType, int exponent> class power { typedef Val</typename>…

続き

C++

どうもvector3_t、要するに3Dで使われるような座標のベクトルだった模様。 まぁIntel386系のコンパイラだとfloatは大体sizeof(float)==4なので3つ組だと12バイトなので16バイトアライメントにはなりませんなぁ。 #include <allocator.hpp> #include <vector> #include <iostream> struct </iostream></vector></allocator.hpp>…

アライメントアロケータ その弐

C++

こちらでアライメントされてるvectorのvectorがコンパイルエラーになるけれどカスタムアロケータならいけるのか聞かれたので早速実験してみるテスト #include <allocator.hpp> #include <vector> #include <iostream> struct hoge { char fuga[16]; }; int main() { typedef std::vector<hoge, risa_gl::aligned_allocator<hoge, 16> > ali</hoge,></iostream></vector></allocator.hpp>…

アライメントされたメモリアロケータ

C++

メモリのアライメントでアライメントされたアロケータのアイデアがかかれてたのを実装したのが手元にあったので公開mmxとか用に使おうとおもって作ったやつがあるんですがよくよく調べてみたらどうせ拡張命令でしか使わないのであればmmxのintrinsics用ヘッ…