How do I apply a complicated function to a template argument of one function template, so that it can be used as a template argument in another function template? I am new to templates, so please bear with me.
In the code below I call foo<1,2>()
, this generates a function, and inside the body of that function, it calls a generated bar<2>()
function. The problem for me is that I have a complicated function that, as far as I know, cannot be evaluated with the standard arithmetic operators.
I can use c++11, if it helps.
#include <iostream>
template<size_t c>
unsigned int bar(){
return c;
}
template<size_t a, size_t b>
unsigned int foo(){
return bar<a*b>();
//return bar<COMPLICATEDFUNCTION(a,b)>(); // what I actually want to do
}
int main(void) {
std::cout << foo<1,2>();
return 0;
}
If it helps, my specific function is f(r,c) = r*c - c - (c choose 2) which counts the number of lower-diagonal elements of a nonsquare matrix (r > c). The function I wrote to compute this could be declared inline
, but as I understand it, the compiler does not promise this. In a previous question, I asked about using macro functions, and that was met with skepticism. Also, in that same thread, constexpr functions were suggested.
Aucun commentaire:
Enregistrer un commentaire