Is it possible to specialize a template class by argument instead by type?
The target is to have a comfortable interface and as less as possible execution time. At the moment I know three options with different benefits.
- One Class with different beaviour depending on CTor Parameter
- Define different classes
- Specialized template classes
The first possibility does not fit the execution time requirement.
The second possibility does not have a nice interface, because everybody has to know that there are multiple classes with just slighly different behaviour.
The third solution is my favorit, but therefore it is needed to declare types as switches.
So I am looking for a mixture between 1. and 3.
I may be missing the correct keywords to search with.
Here the possibilities I know so far:
#include <iostream>
/**
* opt1 -- switch at runtime
*/
class inoutslow
{
public:
inoutslow(bool b): _switch(b)
{
if(_switch)
std::cout << "slowIn1" << std::endl;
else
std::cout << "slowIn2" << std::endl;
}
~inoutslow()
{
if(_switch)
std::cout << "slowOut1" << std::endl;
else
std::cout << "slowOut2" << std::endl;
}
private:
bool _switch;
};
/**
* opt2 -- different self defined classes
*/
class inout1
{
public:
inout1(){std::cout << "in1" << std::endl;}
~inout1(){std::cout << "out1" << std::endl;}
};
class inout2
{
public:
inout2(){std::cout << "in2" << std::endl;}
~inout2(){std::cout << "out2" << std::endl;}
};
/**
* opt3 -- specialized template
*/
struct trueType;
struct falseType;
template<typename T>
class inout
{
public:
inout(){std::cout << "DefaultTin" << std::endl;}
~inout(){std::cout << "DefaultTout" << std::endl;}
};
template <>
class inout<trueType>
{
public:
inout(){std::cout << "Tin1" << std::endl;}
~inout(){std::cout << "Tout1" << std::endl;}
};
template <>
class inout<falseType>
{
public:
inout(){std::cout << "Tin2" << std::endl;}
~inout(){std::cout << "Tout2" << std::endl;}
};
int main()
{
inoutslow i(true);
inoutslow j(false);
inout1 ii;
inout2 jj;
inout<trueType> iii;
inout<falseType> jjj;
}