テンプレートの邪道な使い方

例えば、メンバ関数を引数とする関数を使うとき、templateを使うと簡単に書ける。


#include

using namespace std;

class CInt {
int n;
public:
CInt(int m) { n = m; }
void print() const { cout << n << endl; }
};

template
void func(CInt *c, T f) {
(c->*f)();
}

int main() {
CInt *c = new CInt(1);
func(c, &CInt::print);
delete c;
}

型を書かなくてもコンパイラがやってくれる。
本当はこう書かなくてはいけないらしい。


void func(CInt *c, void (CInt::*f)() const) {
(c->*f)();
}