vendredi 12 juillet 2019

Error in passing function to function template

I have function templates :

template<typename T>
inline T fun3(T &x1, T &x2) 
    {
        return std::pow(x1,2.0) + std::pow(x2,2.0);
    }

template<typename T, typename U>
inline T fun5(U &a)
    {
        return (T(4.0+a*(-2.0),5.0+ a*3.0));
    }

template<typename F, typename T>
void min(F fun1, T& v)
{
    double x={10.0};
    v=fun1(x);
}

int main()
{
    double val;
    min(fun3(fun5),val);
    std::cout<<"value = "<<val<<"\n";
    return 0;
}

I want to evaluate fun3(fun5(x)) and have functions as shown above. But getting error as no matching function for call to ‘Function5<double>::fun5(<unresolved overloaded function type>)’ obj1(o5.fun5(o3.fun3),-2.0,0.0,location,value);

Can someone explain how can I pass function to min()?

What will change if all these functions were class templates like:

template<typename T>
class Fun3 {
    inline T fun3(T &x1, T &x2) 
        {
            return std::pow(x1,2.0) + std::pow(x2,2.0);
        }
};

template<typename T, typename U>
class Fun5 {
    inline T fun5(U &a)
        {
            return (T(4.0+a*(-2.0),5.0+ a*3.0));
        }
};

template<typename F, typename T>
class Min {
    void min(F fun1, T& v)
    {
        double x={10.0};
        v=fun1(x);
    }
};

int main()
{
    double val;
    Fun5<double> o5;
    Fun3<decltype (o5.fun5)> o3;
    Min<???,decltype (o5.fun5)> obj; //What is here?
    obj(o3.fun3(o5.fun5),val);
    std::cout<<"value = "<<val<<"\n";
    return 0;
}

I don't know what will go to commented line. How can I use a function object (functor) here?

Aucun commentaire:

Enregistrer un commentaire