jeudi 21 octobre 2021

How to replace "float (*fSqrt)(float)" with "std::function

I have a function with signature:

template<class Type> 
bool isPrime(const Type& n,float (*fSqrt)(float),bool debug = false)

Which works fine. But,

template<class Type> 
bool isPrime(const Type& n,std::function<float(float)> fSqrt,bool debug = false)

causes compile-error.

How to replace float (*fSqrt)(float) with std::function<float(float)> fSqrt ?
Please note: my ultimate objective is std::function<float(Type)>, where Type is templated.

Wandbox (https://wandbox.org/) shows:

prog.cc: In function 'int main()':
prog.cc:91:28: error: no matching function for call to 'isPrime(int&, <unresolved overloaded function type>, bool)'
   91 |         std::cout<<(isPrime(n,std::sqrt,true)?"Positive":"Negative")<<'\n';
      |                     ~~~~~~~^~~~~~~~~~~~~~~~~~
prog.cc:10:27: note: candidate: 'template<class Type> bool isPrime(const Type&, const float&, bool)'
   10 | template<class Type> bool isPrime(const Type& n,const float& nSqrt = 0.0,bool debug = false) {
      |                           ^~~~~~~
prog.cc:10:27: note:   template argument deduction/substitution failed:
prog.cc:91:28: note:   cannot convert 'std::sqrt' (type '<unresolved overloaded function type>') to type 'const float&'
   91 |         std::cout<<(isPrime(n,std::sqrt,true)?"Positive":"Negative")<<'\n';
      |                     ~~~~~~~^~~~~~~~~~~~~~~~~~
prog.cc:81:27: note: candidate: 'template<class Type> bool isPrime(const Type&, std::function<float(float)>, bool)'
   81 | template<class Type> bool isPrime(const Type& n,std::function<float(float)> fSqrt,bool debug = false) {    // Type & std::function - compile-error
      |                           ^~~~~~~
prog.cc:81:27: note:   template argument deduction/substitution failed:
prog.cc:91:28: note:   cannot convert 'std::sqrt' (type '<unresolved overloaded function type>') to type 'std::function<float(float)>'
   91 |         std::cout<<(isPrime(n,std::sqrt,true)?"Positive":"Negative")<<'\n';
      |                     ~~~~~~~^~~~~~~~~~~~~~~~~~

OnlineGDB (https://www.onlinegdb.com/#) shows:

main.cpp:91:38: error: no matching function for call to ‘isPrime(int&, , bool)’
  std::cout<<(isPrime(n,std::sqrt,true)?"Positive":"Negative")<<'\n';
                                      ^
main.cpp:10:27: note: candidate: template bool isPrime(const Type&, const float&, bool)
 template<class Type> bool isPrime(const Type& n,const float& nSqrt = 0.0,bool debug = false) {
                           ^~~~~~~
main.cpp:10:27: note:   template argument deduction/substitution failed:
main.cpp:91:38: note:   cannot convert ‘sqrt’ (type ‘’) to type ‘const float&’
  std::cout<<(isPrime(n,std::sqrt,true)?"Positive":"Negative")<<'\n';
                                      ^
main.cpp:81:27: note: candidate: template bool isPrime(const Type&, std::function, bool)
 template<class Type> bool isPrime(const Type& n,std::function<float(float)> fSqrt,bool debug = false) {    // Type & std::function - compile-error
                           ^~~~~~~
main.cpp:81:27: note:   template argument deduction/substitution failed:
main.cpp:91:38: note:   cannot convert ‘sqrt’ (type ‘’) to type ‘std::function’
  std::cout<<(isPrime(n,std::sqrt,true)?"Positive":"Negative")<<'\n';
                                      ^

Aucun commentaire:

Enregistrer un commentaire