I would like to know if it is possible to specialize the operator()
:
I would like to be able to call different variants of operator()
at compile time. The code bellow provides an example of what is expected.
#include <vector>
#include <iostream>
#include <string>
#include <numeric>
#include <array>
enum OpSelector {stdOp, Op1, Op2};
typedef std::array<double, 3> vec;
template<class T, OpSelector S = stdOp>
class classA
{
public:
classA() {}
double operator()(const double& a, const T& b) const
{
return (a & b);
}
};
// Default function
template<class T>
double operator&( const double& a, const T& b )
{
std::cout << "Operation 0" << std::endl;
return 0;
}
// Specialized function
template<>
double classA<vec, Op1>::operator()(const double& a, const vec& b) const
{
std::cout << "Operation 1" << std::endl;
return 1;
}
// Specialized function
template<>
double classA<vec, Op2>::operator()(const double& a, const vec& b) const
{
std::cout << "Operation 2" << std::endl;
return 2;
}
int main(int argc, char** argv)
{
classA<vec> obj;
vec v {1,2,3};
obj(1.2, v); // works
obj<Op1>(1.2, v); // Does not work
return 0;
}
Is it possible to achieve this through specialization/ some other means?