So in C++ we can pass functions as template parameters like so:
template <typename func>
int tester(func f)
{
return f(10);
}
So I have a template class which uses a function, and I've been using it like this, where each time I would pass the function and a variable to it.
template <typename type_t>
class TCl
{
...
template <typename func>
int Test(func f, type_t Var>
{
// Blah blah blah
}
};
But I would like to make the function one of the class template parameters to make it easier to use. So I first tried this:
template <typename func>
template <typename type_t, func f>
class TCl
{
...
};
But when I compile this I get:
Compiler Error C3857: multiple type parameter lists are not allowed
Because God forbid my code should actually compile the first time I try it.
Now my problem has been that while I know the parameters types for the function (in this case size_t, size_t), the return type could be anything, so long as there is a proper comparison operator for type_t.
After several hours of reading online, I found a working solution.
template <typename type_t, typename ret_t, ret_t f(size_t, size_t)>
class TCl
{
...
};
While it work, it kind of ruins the aesthetic of the code. And I would greatly prefer something like in the original example where I specify a func type, and don't have to worry about specifying the return type.
So does anyone have any suggestions?
Also, no Boost.
EDIT: SOLVED!
Thank you guys. I used Jarod42's solution.
@Drax & Lightness Races in Orbit:
I had considered placing the type before hand, but it would have placed the burden on the programmer to define the function pointer in order to use it, which seemed unnecessarily cruel :)
Jarod42 however solved that using macros and the decltype operator, which I had completely forgotten about.
There was a slight problem with your example Jarod42.
#define TCl_T(type_t, function) Tcl<type_t, decltype(function), function>
Generates an error: error C1001: An internal error has occurred in the compiler.
This solves it.
#define TCl_T(type_t, function) Tcl<type_t, decltype(&function), function>
Apparently we have to specify that it is a pointer.
Once again, thanks for the help guys!