I have a class template that intends to use its parameter K as the key to a map.
Is there any way to restrict the template parameter to be a type that complies with the Key in std::map?
I realize that even without such constraint, the compiler would spit out a pile of template errors like K having no operator < (), but it would be nice if I can make my code more obvious in specifying requirements.
C++11 solutions are welcome.
template< typename K >
class Foo
{
// lots of other code here...
private:
std::map< K, size_t > m_map;
};
It depends on what you mean by “complies.” If you want to verify that
Khas a<operator then you might try the Boost Concept Checking Library.However if you want to verify that
<defines a StrictWeakOrdering onK, that would require testing run-time behaviour under all possible inputs.