I have struct Opers
with some arithmetic operations: mult()
, div()
, mod()
.
And I need to specialize template for certain values of n
. Here is example for Opers<1>
.
But, also I want to do specialization for n
that are powers of 2 ( n = 2,4,8,16, ...) – in this case I can optimize operations mult()
and div()
(using bitwise shift left or right).
#include <iostream>
using namespace std;
template<int n> struct Opers {
int mult(int x){
return n*x;
}
int div(int x){
return x / n;
}
int mod(int x){
return x % n;
}
};
template<> struct Opers<1> {
int mult(int x){
return 1;
}
int div(int x){
return x;
}
int mod(int x){
return 0;
}
};
int main() {
Opers<1> el2;
cout << el2.mult(3) <<endl;
}
I'm looking for construction like
template<> struct Opers<isPowerOfTwo()>
int mult(int x){
// do smth
}
Is it possible or what manual should I read?
UPD. Using C++11 is allowed, and even would be better.