No Such Blog or Diary
C++ であるクラスを継承しているかどうかの判定って
できるかなぁと.
#include <iostream> using namespace std; template <typename E> class Eq { virtual bool operator==(const E& e) const = 0; }; class Int : public Eq <Int> { public: int num; bool operator==(const Int& e) const { return this->num == e.num; } Int(int num) { this->num = num; } }; template <typename A> struct HasEq { enum {val = 0}; }; template <typename A> struct HasEq< Eq<A> > { enum {val = 1}; }; int main(int argc, char *argv[]) { cout << HasEq<Int>::val << endl; return 0; }
このコードの出力が 1 になってほしいと.このコードでは 0 を出力するほうに実体化されるので,これを避けたい.キャストを入れて 0 出力側で実体化を失敗させればいいのかな?
- Comments: 0
- TrackBack (Close): -
unistd.h って...
http://acm.uva.es/problemset/">UVa Online Judge で C++ 使ってて C の read 関数を使おうとしたら
#include <unistd.h>
が抜けててコンパイルエラーを食らった.fread は stdio.h にあるのに read は unistd.h なのね.手元の g++ では unistd.h を明示的に読まずにコンパイルできてしまう点が痛い...
- Comments: 0
- TrackBack (Close): -
関数ポインタの判別?
C++ のテンプレートでテンプレート引数に関数ポインタが指定されたときだけ挙動を変えたい.でもやり方が良くわからない... ポインタかどうかの判定法は知ってるが,関数ポインタか否かはどうすれば?
- Comments: 0
- TrackBack (Close): -
vector<int> と vector<bool>
動作に全く違いのない二つのプログラムに結構な速度差が出てるから原因はなんだろうとしばし悩む.プログラムの動作はまったく同じなのに... んでよく見てみたら vector の型がちと違っていた.vector<int> で使ってるほうが速く,vector<bool> が遅かった.キャッシュに乗り切ってしまうサイズだったし int でのアクセスのほうが機械にやさしかったと.こんなんで倍速くなるとは少々驚きの結果だった.
- Comments: 0
- TrackBack (Close): -
std::cout のっとり
MPI使ってるときにルートプロセスだけ cout で情報を出力するように cout をのっとってみた.副作用として iomanip がどうやって処理されているかを身をもって知る羽目になった(一番下のテンプレート定義だけだとうまくコンパイルできなかった...)
class myOstream : public std::ostream { public: // for iomanip myOstream& operator<<(__ostream_type& (*manip)(__ostream_type& if(rank==0) std::cout << manip; return *this; } myOstream& operator<<(__ios_type& (*manip)(__ios_type&)){ if(rank==0) std::cout << manip; return *this; } myOstream& operator<<(ios_base& (*manip) (ios_base&)){ if(rank==0) std::cout << manip; return *this; } // for other types template <typename C> myOstream& operator<<(const C&v){ if(rank==0) std::cout << v; return *this; } }; muOstream cout;
型を決定できないのかねぇ.ちゃんと書いといてやらんと.
- Comments: 0
- TrackBack (Close): -