I want to learn C++ templates and metaprogramming.
For this aim, I created two classes, BClass and JClass, and the aim is to work with standard containers using the same or similar operators or functions (e.g. insert, operator[], …).
However I get into important compiler errors.
My try at that was:
template<bool B, typename T> class ComparePtr { public: ComparePtr(); using Enable_if = std::enable_if<B, bool>::type; Enable_if<Is_class<T>(), bool>> operator() (T& t1, T& t2) { return (t1.id < t2.id); } }; template<bool B, typename T> ComparePtr<B, T>::ComparePtr() { }
`template <typename KeyType, typename ValType, typename Comp>
ValType& customOperatorAccess(std::unordered_map<KeyType, std::vector<ValType>, Comp>& myMap, const KeyType& key) {
auto it = myMap.find(key)
if(it != myMap.size()) {
//Found
return it->second;
}
throw std::out_of_range("Key not found in customOperatorAccess");
}`
Then in another file, I called the function as:
void JClass::find_(const CClass& c) { std::unordered_map<JClass, std::vector<const BClass>, ComparePtr<true, BClass>> tmap; tmap = /*found elsewhere*/ const BClass fnd = customOperatorAccess(tmap, this); /*...rest...*/ }
Looks like the compiler does not like the implementation (no instance of function template “customOperatorAccess” matches the argument list). Is there a better way to implement this?
Thanks
DP92_A_Yellow is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.