How to make template class Collection<K,T>
receive a function T
- that can either has signature T(K)
or T(K,int)
- as template argument, then conditionally compile base on the signature of the function?
Here is the existing code that can receive 1 signature : Collection<K,HashFunction(K)>
.
template<typename AA> using HashFunction= HashStruct& (*)(AA );
/** This class is currently used in so many places in codebase. */
template<class K,HashFunction<K> T> class Collection{
void testCase(){
K k=K();
HashStruct& hh= T(k); /*Collection1*/
//.... something complex ...
}
};
I want it to also support Collection<K,HashFunction(K,int)>
.
template<class K,HashFunction<K> T /* ??? */> class Collection{
int indexHash=1245323;
void testCase(){
K k=K();
if(T receive 2 parameter){ // ???
HashStruct& hh=T(k,this->indexHash); /*Collection2*/ // ???
//^ This is the heart of what I really want to achieve.
//.... something complex (same) ...
}else{
HashStruct& hh=T(k); /*Collection1*/
//.... something complex (same) ...
}
}
};
Do I have no choice but to create 2 different classes : Collection1
& Collection2
?
Answer that need more than c++11 is ok but less preferable.
I feel that it might be solvable by using "default parameter" trick.