C++

clang3.2(trunk) on MinGW(MSYS) with GCC 4.7.0

clangのコンパイル方法は日本語でもあちこちにあるので省略。 現時点のsvn trunkからcheak outしたソースを使用した場合、 MinGWでGCC4.7.0を使っていると色々と問題が出るのでその修正方法を書きます。1.Include Pathの解決(既に知っている人が多いと思い…

プログラミング楽しい

C++

おひさしぶりです。 今回はstd::bindを使ってネストしたクラスのメンバ変数を束縛してみます。 以下サンプル #include <iostream> #include <vector> #include <functional> // std::bind std::placeholders::_1 #include <algorithm> // std::for_each struct Hoge { Hoge() : i(1) {} int i; }; stru</algorithm></functional></vector></iostream>…

C++でPropertyつづき

C++

前のエントリーに上書きしちゃったみたいです。汗 Boost.Propertyの実装案みたいなもの->http://codepad.org/A4vaAMPZ これみて簡単にならないかやってみました。 #include <boost/typeof/typeof.hpp> #include <iostream> template<typename T> struct property_get_type; template<typename ReturnType,typename Class> struct property_get_type</typename></typename></iostream></boost/typeof/typeof.hpp>

C++でPropertyつづき1

C++

self()呼び出しもできた。http://codepad.org/eKAdqttP 以下に同じコードを #include <boost/typeof/typeof.hpp> #include <iostream> template<typename T> struct property_get_type; template<typename ReturnType,typename Class> struct property_get_type<ReturnType (Class::*)() const> { typedef ReturnType type; }; template</returntype></typename></typename></iostream></boost/typeof/typeof.hpp>

もしもBoost.Threadにinterruptが無かったら。

C++

VC限定なコードですけが、こんな感じで追加できるんじゃないかと。 元ネタはtwitterでid:melponさんがstd::threadにinterruptが無いので欲しいってとこからです。 id:melponさんがつぶやいてたコードの大部分借りました。 #include "stdafx.h" #include <iostream> #pr</iostream>…

もはやBoost.ScopeExit・・・

C++

#include <iostream> #include <boost/typeof/typeof.hpp> #include <boost/preprocessor/seq/cat.hpp> #include <boost/preprocessor/seq/for_each_i.hpp> // Steven Watanabe's trick namespace closure { namespace args { template<int> struct declare; typedef void* declared; struct undeclared { declared dummy[2…</int></boost/preprocessor/seq/for_each_i.hpp></boost/preprocessor/seq/cat.hpp></boost/typeof/typeof.hpp></iostream>

boost::filesystemを使ってある拡張子のファイルの先頭行に文字列を

C++

ちょっと必要になったので書いてみただけです。>< #include <iostream> #include <string> #include <boost/filesystem.hpp> #include <boost/filesystem/fstream.hpp> #include <boost/xpressive/xpressive.hpp> void replace_file( const boost::filesystem::path& dir, const std::string& inset_str) { try { namespace fs = boost::filesyste…</boost/xpressive/xpressive.hpp></boost/filesystem/fstream.hpp></boost/filesystem.hpp></string></iostream>

ちょっとだけクロージャを作ってみるのメモ

C++

#include <iostream> #include <vector> #define CLOSURE_(A0) CLOSURE_DEF(##A0, __LINE__) #define CLOSURE_DEF(A0, id) CLOSURE_IMPL(A0, id) #define CLOSURE_IMPL(A0, id)\ struct Closure_##id {\ typedef decltype(A0) Arg0;\ Arg0& A0;\ Closure_##id(Arg0& a0) : A0(a</vector></iostream>…

N2855 noexceptについて

C++

C++0xの新しい提案でN2855(Rvalue References and Exception Safety)では、noexceptというキーワードが追加されています。 まずはコードから見ます。 noexcept int foo(int); int bar(int); noexcept void wibble(int x, int y) { x = foo(x); // OK: foo()…

練習でstd::reference_wrapperがラップしてる型を取得するメタ関数を作ってみた

C++

std::reference_wrapperの場合は保持してるTを返します。 std::reference_wrapperではない場合は受けた型をそのまま返します。 #include <functional> #include <tuple> namespace mpl { template<class T> struct remove_wrap; { typedef T type; }; template<class T> struct remove_wrap<std::reference_wrapper<T>> { typ</std::reference_wrapper<t></class></class></tuple></functional>…

ユーザ定義リテラルまとめ

C++

このエントリは見事に間違ってたので忘れてください。>< グローバルな名前空間でもユーザ定義リテラルは定義できます。参考 http://cpplover.blogspot.com/2009/09/user-defined-literal.htmlところで新たな疑問があったのでここにメモ。 template <char... C> std::s</char...>…

C++テンプレートテクニック

C++

やっとレビューを書きます。id:faith_and_braveさんとεπιστημηさんの共著 C++テンプレートテクニック この本はタイトルの通りC++のテンプレートを使ったテクニック集になってます。 対象の読者は中級以上といったところでしょうか。 STLを使ってプログラミン…

mpl勉強モード

C++

enable_ifを使ってテンプレートメンバー関数を特殊化してみた? #include <iostream> #include <string> #include <typeinfo> #if defined(__GNUC__) #include <type_traits> #include <cxxabi.h> #endif #if !defined(__GNUC__) template <bool b, class T> struct enable_if; template <class T> struct enable_if<true, T> { typedef T type; }; #…</true,></class></bool></cxxabi.h></type_traits></typeinfo></string></iostream>

Windows版gcc4.4でC++0xのコードをコンパイルに`c++0x'だけではダメ

C++

コンパイルオプションに`-std=c++0x'だけではダメで、`gnu++0x'も指定する必要があります。 (もしくは`gnu++0x'だけでも良いかもしれません。) 結論としては`-std=c++0x'ではなく、`-std=gnu++0x'を指定しなければいけません。 C++0xのコードをコンパイルす…

クラスのポインタをvoid*でdeleteした場合デストラクタが呼ばれない

C++

意外と知らない人が多かったので簡単なサンプル #include <iostream> struct Hoge { ~Hoge() { std::cout << __FUNCTION__ << std::endl; } }; int main() { Hoge* p = new Hoge; void* pv = p; delete pv; return 0; } 実行結果 gcc4.4 実行結果 VC8 必ず正しいクラス</iostream>…

Erase-Remove Ideomを少しでも楽に使うために。(自分用メモ)

C++

こんな感じでコンテナの値を削除できるようにしてみた。 #include <algorithm> #include <vector> struct Hoge { int num_; explicit Hoge(int i) : num_(i) {} int getnum() { return num_; } }; int main(int argc, char** argv) { std::vector<Hoge> v; v.push_back(Hoge(1)); v.pu</hoge></vector></algorithm>…

boost::threadとstd::thread周りを調べてたら

C++

こんな記事を見つけました。 http://msdn.microsoft.com/ja-jp/magazine/cc163405.aspx 以下記事内に書かれているWindows Vista以降で使用可能な関数とPosixとの対応表を作ってみました。 PosixAfter Windows Vista pthread_cond_initInitializeConditionVar…

__FUNCTION__マクロのwchar_t版

C++

VCでは char版は__FUNCTION__ wchar_t版は__FUNCTIONW__ ほかには__FILE__も__FILEW__が存在します。 xutilityヘッダに以下のように定義されてます。 #define __STR2WSTR(str) L##str #define _STR2WSTR(str) __STR2WSTR(str) #define __FILEW__ _STR2WSTR(_…

N2800 P29の一部を訳してみました。

C++

原文§ 2.13.7の2 2 A user-defined-literal is treated as a call to a literal operator or literal operator template (13.5.8). To determine the form of this call for a given user-defined-literal L with ud-suffix X, the literal-operator-id whos…

まだ考えてました。

C++

#include <tuple> #include <iostream> #include <string> #include <functional> #include <stdexcept> void printf(const char *s) { while (*s) { if (*s == '%' && *(++s) != '%') throw std::runtime_error("invalid format string: missing arguments"); std::cout << *s++; } } template</stdexcept></functional></string></iostream></tuple>

こんなのしか思いつかなかった。

C++

可変引数を保持しておいてそれを別な関数に渡すには? やりたい事と違うかもしれませんが #include <tuple> #include <iostream> #include <string> template <class T> void do_something(const T& t) { std::cout << t << std::endl; } template <class Tuple, size_t N> struct do_tuple_helper { static void do_al</class></class></string></iostream></tuple>…

threadクラスの実装を晒す2

C++

日にちがあいてしまいましたがエラー処置と引数を追加していきます。 コンストラクタで行っていた処理をstart_thread関数に移しています。 それに伴いthread_base*をメンバー変数p_にしました。 // thread.hpp #include <memory> // std::auto_ptr #include <process.h> // ::_b</process.h></memory>…

前回雑記で書いたthreadクラスの実装を晒す

C++

実装を見る前にもう一度threadクラスの使い方を見ておきます。 #include <iostream> #include "thread.hpp" // これから実装を見ていくthreadクラスのヘッダファイル void foo() { std::cout << "fooを実行しています" << std::endl; } int main() { thread t(foo); //</iostream>…

boostのrefが必要になったので作ってみた

C++

boostにあるref相当の物が必要になりました。 なので作ってしまいます。 template class reference_holder { T& val_; public: reference_holder(T& arg) : val_(arg) {} // Tのリファレンスを保持 operator T&() { return val_; } // 型変換を利用してTのリ…

ISO/IEC 14882:2003 PDFをみつけた。

C++

これってまずいんじゃないですか?(^-^; それとも偽者? http://code.google.com/p/openassist/downloads/detail?name=C%2B%2B%20Standard%20-%20ANSI%20ISO%20IEC%2014882%202003.pdf&can=2&q=

gcc4.4のthreadがコンパイルできない

C++

Windows版だけかもしれませんが、C++0xのヘッダは以下の定義が入っていてclass threadが使えなかったです。 #if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1) これらはで定義されてます。 /* Define if gthreads library is ava…

opensslにAES暗号化ライブラリがあるのを知ったのでこんな形で利用できるようにラップしてみました。

C++

キーとIVの設定 aes::aes_cbc cbc_key(key, iv); エンコードするとき // AES-CBC encode // サイズが0以下の場合throw bad_aes_cast aes::byte_string enc_buf = aes::aes_encode_cast(cbc_key, data); デコードする時 // AES-CBC decode // デコードしたい…

シングルトンについて

C++

結論から言うとDouble Checked Lock使う場合はメモリバリアを入れなさいと。 そうでないならThread Local Storageを使って実装しなさいなのですねー。 [参考1]The "Double-Checked Locking is Broken" Declaration [参考2]Binary Hacks ―ハッカー秘伝のテク…

う〜ん

C++

Windows版gcc4.4の-fthreadsafe-staticsは動かないなぁ〜 tlsの__threadはうまく動いてるっぽいのに。 Thread-local storage NOだからtlsも動いてないぽい? memory barrierはうまく動いてるのかどうかわからない。 特にVCの_ReadWriteBarrier系は使う必要あ…

のりおくれたー

C++

cppll_noviceに今日流れてたStackとHeapの測定は面白かったなー 参加したかったけど仕事してました。一応測定ネタとして。 Windowsの場合は以下のAPIを使用して計測するのが良いです。 SetThreadAffinityMask ← 実行するプロセッサを固定するために使用する …