No Such Blog or Diary

«Prev || 1 | 2 | 3 |...| 8 | 9 | 10 |...| 12 | 13 | 14 || Next»

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 出力側で実体化を失敗させればいいのかな?

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 を明示的に読まずにコンパイルできてしまう点が痛い...

逆さにしても英単語になる英単語を

Aspell の英語辞書から単語を抜き出して全探索してみた.登録されている単語数は 141891 らしい.んで,1文字の単語を抜いてさかさまにしても単語があったのは 381 個.うち palindrome になっているものが 112 個.自身にもどらずほかの単語になるのが 269 個.さらに長さが4以上であるものは 137 個(これらの結果は複数形の s や動詞の変化をはじいていない)

よく知った単語の例:

emit ⇔ time

evil ⇔ live

flow ⇔ wolf

keep ⇔ peek

loop ⇔ pool

part ⇔ trap

プログラム結果

関数ポインタの判別?

C++ のテンプレートでテンプレート引数に関数ポインタが指定されたときだけ挙動を変えたい.でもやり方が良くわからない... ポインタかどうかの判定法は知ってるが,関数ポインタか否かはどうすれば?

vector<int> と vector<bool>

動作に全く違いのない二つのプログラムに結構な速度差が出てるから原因はなんだろうとしばし悩む.プログラムの動作はまったく同じなのに... んでよく見てみたら vector の型がちと違っていた.vector<int> で使ってるほうが速く,vector<bool> が遅かった.キャッシュに乗り切ってしまうサイズだったし int でのアクセスのほうが機械にやさしかったと.こんなんで倍速くなるとは少々驚きの結果だった.

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;

型を決定できないのかねぇ.ちゃんと書いといてやらんと.

«Prev || 1 | 2 | 3 |...| 8 | 9 | 10 |...| 12 | 13 | 14 || Next»
Search
Feeds

Page Top