I was thinking about about template specialization and was wondering if there was a way to use partial specialization to generate two different code paths that are automatically merged.
In this case I have a computational engine where I want an enum to select different functions at compile-time. In this case, depending on the policy or scheme I want different functions at compile-time.
I was thinking that I could avoid explicitly partially specializing all variants. Is this possible?
I've included a small code sample.
#include <iostream>
enum class scheme { linear, polynomial };
enum class policy { no_checking, raise_exception };
struct computational_base
{
void left();
void middle();
void do_stuff()
{
left();
middle();
}
};
template <scheme scheme, policy left>
struct computational_backend : public computational_base
{
};
template <policy left>
struct computational_backend<scheme::linear, left> : public computational_base
{
void middle()
{
std::cout << "scheme::linear" << std::endl;
}
};
template <scheme scheme>
struct computational_backend<scheme, policy::no_checking> : public computational_base
{
void left()
{
std::cout << "policy::no_checking" << std::endl;
}
};
int main()
{
//Ideally would select middle() from first template, and left() from second template
// more than one partial specialization matches the template argument list
computational_backend<scheme::linear, policy::no_checking> what;
what.do_stuff();
return 0;
}