f ma być klasą - masz funkcję.
Patrz poniżej:
// Class acts like a function - also known as functor.
template<typename T, int a, int b>
class f
{
int operator()(T v)
{
return v*a-b; // just do something for example
}
};
template<typename T, int a, int b, template<typename,int,int> class func>
class C
{
int f()
{
return func<T,a,b>(3);
}
};
int main()
{
C<float,3,2, f> c;
}
... A wersja przystosowana jeśli trzeba kod portu starszych (Dostosowuje funkcji do szablonu klasy):
#include <iostream>
template<typename T, int a, int b>
int f(T v)
{
std::cout << "Called" << std::endl;
return v*a-b; // just do something for example
}
template<typename T, int a, int b, template<typename,int,int> class func>
struct C
{
int f()
{
return func<T,a,b>(3);
}
};
template <class T, int a, int b>
struct FuncAdapt
{
T x_;
template <class U>
FuncAdapt(U x)
: x_(x)
{}
operator int() const
{
return f<T,a,b>(x_);
}
};
int main()
{
C<float,3,2, FuncAdapt > c;
c.f();
}
Co dokładnie próbujesz osiągnąć? – nijansen
Mój kompilator zawiesił się podczas wykonywania tego kodu. – Saksham
@nijansen nie jest stabilny MSVS2010? – Saksham