I just encountered a new compilation error in my code after a visual studio update. I have stripped my code down to what I think is a minimal example. I have two templated functions both called pow
– the first takes two parameters of any type and calls std::pow
with them. The second uses a concept to override the first template such that if the second parameter matches the concept it gets called instead.
In my code, I for some reason had the two template parameters in the opposite order to how they appeared in the parameter list. This used to compile, but recently stopped compiling with the error 'pow': ambiguous call to overloaded function
. However, if I swap the order of the template parameters to match the function parameters it compiles fine.
Why would this compile one way and not the other?
Here is the minimal example
#include <cmath>
template<class T, class U>
double pow(const T& base, const U& power)
{
return std::pow(base, power);
}
template<class T>
concept HasValue =
requires(std::remove_cvref_t < T > t)
{
t.value;
};
//this causes an ambiguous call to overloaded function compilation error
template<HasValue T, class U>
double pow(const U& s, const T& a)
{
return std::pow(s, a.value);
}
//this works with no compilation error
//template<class U, HasValue T>
//double pow(const U& s, const T& a)
//{
// return std::pow(s, a.value);
//}
class MyClass
{
public:
double value;
};
int main()
{
MyClass myClass;
myClass.value = 2.0;
pow(10.0, myClass);
}
1