Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Template deduction reversion with function pointer

I've had previously-working code fail on moving from g++-5 to g++-6; a previously-deducible template is no longer deducible. A minimal example:

#include <math.h>

template<typename T,typename T1>
T apply(T (*func)(T1), const T1 &val)
{
  return func(val);
}

int main(void)
{
  double val1 = 0.5, val2 = apply(ceil,val1);
  return 0;
}

g++-6 can't seem to find the correct version of ceil:

foo.cpp: In function ‘int main()’:
foo.cpp:11:44: error: no matching function for call to ‘apply(<unresolved overloaded function type>, double&)’
   double val1 = 0.5, val2 = apply(ceil,val1);
                                        ^
foo.cpp:4:3: note: candidate: template<class T, class T1> T apply(T (*)(T), const T1&)
 T apply(T (*func)(T), const T1 &val)
   ^~~~~
foo.cpp:4:3: note:   template argument deduction/substitution failed:
foo.cpp:11:44: note:   couldn't deduce template parameter ‘T’
   double val1 = 0.5, val2 = apply(ceil,val1);

g++-5 has no problems and works as expected. Using the Compiler Explorer with g++ 8 at https://godbolt.org/z/oBSopG, I also see a reversion from clang-3.3 (compiles) to clang-3.4 (doesn't compile).

Given that the code still doesn't work, even in current g++, I assume that the error is on my part. What have I done wrong and how can I fix it?

like image 635
gilgamec Avatar asked Jan 09 '19 10:01

gilgamec


1 Answers

What have I done wrong and how can I fix it?

The way to fix that is to #include <cmath> instead of using #include <math.h> as mentioned in the reference documentation:

#include <cmath> // <<< official header to use.
#include <iostream>

template<typename T,typename T1>
T apply(T (*func)(T1), const T1 &val)
{
  return func(val);
}

int main(void)
{
  double val1 = 0.5, val2 = apply(ceil,val1);

  std::cout << val1 << ' ' << val2<< std::endl;
}
like image 79
πάντα ῥεῖ Avatar answered Oct 23 '22 05:10

πάντα ῥεῖ