I would like to capture compile time constant integers passed to a function into templated types. The future goal is to roll my own (very limited) expression template that creates expressions from very simple expressions (sums of compile time constant int and in variables) such as: -2*i + 3*k.
I am starting very slowly, I have the following code:
struct Foo {
Foo (int i) : i_(i) {}
int i_;
};
template <int N> int operator*(std::integral_constant<int, N> i, Foo j) {
return N * j.i_;
}
int main(void) {
std::integral_constant<int, 2> k;
int i = k * Foo(3);
cout << i << endl;
int j = 2 * Foo(3);
cout << j << endl;
}
The template argument deduction fails for 2*Foo(3).
I have read that there is no such thing as constexpr parameters to a function.
Is there a way to accomplish what I want ?
You can do that without templates. This answer more or less expands @MooingDuck’s suggestion into concrete code, which you might find easier to understand: