I'm writing a template class, and I want to allow an additional method to exist only for a certain template type. Currently the method exists for all template types, but causes a compilation error for all other types.
Complicating this is that it's an overloaded operator(). Not sure if what I want to do is actually possible here.
Here's what I have now:
template<typename T, typename BASE>
class MyClass : public BASE
{
public:
typename T& operator() (const Utility1<BASE>& foo);
typename T const& operator() (const Utility2<BASE>& foo) const;
};
I want the T&
version always available, but the T const&
version only available if Utility2<BASE>
is valid. Right now, both methods exist, but attempting to use the const version gives a weird compilation error if Utility2<BASE>
is invalid. I'd rather have a sensible error, or even a "no such member function" error.
Is this possible?
EDIT: After reading through the boost docs, here's what I've come up with, and it seems to work:
template<typename T, typename BASE>
class MyClass : public BASE
{
public:
typename T& operator() (const Utility1<BASE>& foo);
template<typename U>
typename boost::enable_if<boost::is_same<Utility2<BASE>, U>, T>::type const &
operator() (const U& foo) const;
};
So that method doesn't exist unless someone tries to use it with Utility2, and they can only create a Utility2 if it's valid for that BASE type. But when it's not valid for that BASE type, MyClass will not waste time creating the accessor method.