I'm trying to specialize a struct template for multiple types at once using SFINAE. I know that something like the following works:
#include <iostream>
template <typename T, typename Enable = void>
struct S {
void operator()() {
std::cout << "Instantiated generic case" << std::endl;
}
};
template<typename T>
using enabled_type = typename std::enable_if<
std::is_same<T, int>::value ||
std::is_same<T, float>::value
>::type;
template <typename T>
struct S<T, enabled_type<T>> {
void operator()() {
std::cout << "Instantiated int/float case" << std::endl;
}
};
int main() {
S<float>()();
return 0;
}
My problem is that I can't modify the primary template of the S
struct to add typename Enable = void
, as it's part of an external header-only
library. So the primary template will have to look like this:
template <typename T>
struct S {
void operator()() {
std::cout << "Instantiated generic case" << std::endl;
}
};
Is there a way that I could still use SFINAE to specialize this template?
Edit: Note that the S
struct is used by code in the external library, so I will have to actually specialize S
and can't subclass it.
Also, the real code I'm working on is much more complicated and would benefit much more from SFINAE than this simple example (I have multiple template parameters that need to be specialized for all combinations of a number of types).
T
you're interested in, this might help: https://mcmap.net/q/377537/-using-sfinae-for-template-class-specialisation – Koto