Home > Archives > 2006年01月05日

2006年01月05日

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

Home > Archives > 2006年01月05日

Search
Feeds

Page Top