Reputation: 28178
Given:
struct A
{
virtual bool what() = 0;
};
template<typename T, typename Q>
struct B : public A
{
virtual bool what();
};
I want to partially specialize what
like:
template<typename T, typename Q>
bool B<T, Q>::what()
{
return true;
}
template<typename Q>
bool B<float, Q>::what()
{
return false;
}
But it appears that this isn't possible (is it in C++11?) so I tried SFINAE:
template<typename T>
typename std::enable_if<std::is_same<T, float>::value, bool>::type B<T>::what()
{
return true;
}
template<typename T>
typename std::enable_if<!std::is_same<T, float>::value, bool>::type B<T>::what()
{
return false;
}
This also doesn't work, I have no idea why though, does anyone? So I found this thread and ended up with:
template<typename T, typename Q>
struct B : public A
{
virtual bool what()
{
return whatimpl(std::is_same<T, float>());
}
bool whatimpl(std::false_type)
{
return false;
}
bool whatimpl(std::true_type)
{
return true;
}
};
This final solution works, but why doesn't the enable_if
technique work? I'm also very open to suggestions of a cleaner answer that I haven't encountered yet.
I simplified my examples as much as possible - in my real use case what()
isn't called what and actually does a fair bit of work, and I'll want to 'specialize' on a user defined type, not float
.
Upvotes: 25
Views: 16482
Reputation: 1
You can use static function an templatize it, like this:
#include <iostream>
using namespace std;
struct A
{
virtual bool what() = 0;
};
template <typename T, typename Q>
static bool what()
{
cout << "T: " << typeid (T).name() << " Q: " << typeid (Q).name() << endl;
return sizeof (T) < sizeof (Q);
}
template <typename Q>
static bool what(float t, Q q)
{
cout << "T: " << t << " Q: " << q << endl;
}
template<typename T, typename Q>
struct B : public A
{
virtual bool what()
{
return ::what<T, Q>();
}
};
int main()
{
B<int, char> bic;
B<float, char> bfc;
cout << bic.what() << endl;
cout << bfc.what() << endl;
return 0;
}
Upvotes: 0
Reputation: 3256
Two possible solutions depending on your use case:
P.S. I'm trying to add the code here directly but there is some formatting problem. If someone could help me out with adding the formatted code from Coliru it'd be great.
Upvotes: 0
Reputation: 3166
Partial specialization is explicitly permitted by the standard only for class templates (see 14.5.5 Class template partial specializations)
For members of class template only explicit specialization is allowed.
14.7 (3) says:
An explicit specialization may be declared for a function template, a class template, a member of a class template or a member template. An explicit specialization declaration is introduced by template<>.
So any definition starting with
template<typename T>
is not an allowed syntax for member of class template specialization.
[edit]
As to SFINAE attempt, it failed because actually there are neither overloads nor specializations here (SFINAE works while defining a set of candidate functions for overload resolution or while choosing proper specialization). what() is declared as a single method of class template and should have a single definition, and this definition should have a form:
template<typename T, typename Q>
B<T,Q>:: bool what(){...}
or may be also explicitly specialized for particular instantiation of class B:
template<>
B<SomeParticularTypeT,SomeParticularTypeTypeQ>:: bool what(){...}
Any other forms are syntacticaly invalid, so SFINAE can't help.
Upvotes: 13
Reputation: 299
Why not just change it to..
template<typename T, typename Q>
struct B : public A
{
bool what()
{
return false; //Or whatever the default is...
}
};
template<typename Q>
struct B<float, Q> : public A
{
bool what()
{
return true;
}
};
Upvotes: 2