0

I have a generic type for my class, but the generic type itself needs to implement the hash() function. Can I require this in my class declaration?

I can easily just make sure I put in a hash() function into the class I pass to myClass, but I would like to get a compilation error if it doesn't exist.

template <typename _T>
class myClass
{
    _T* list;
public:
    myClass(){
        this->list = (_T*)malloc(10,sizeof(_T));
        for(int i = 0;i < 10; i++){
            this->list[i]=_T(i);
        }
    }
    unsigned int doTheThingIWant(){
        return this->list[0].hash()
    }
};

Does anyone know how to force the _T type to have hash() implemented?

  • Possible duplicate of [Is it possible to write a template to check for a function's existence?](https://stackoverflow.com/questions/257288/is-it-possible-to-write-a-template-to-check-for-a-functions-existence) – Mike Kinghan Jan 25 '19 at 12:02
  • 1
    Just simply call the function the way you do, and the compiler will scream if it's not there. In what way does the code you show fail to achieve your goal? – Igor Tandetnik Jan 26 '19 at 04:16

0 Answers0