I am getting a SFINAE error with the following code as it will not use the template. I am trying to perfect forward the args in as the result. Any ideas anyone.
#include <iostream>
#include "constants.h"
namespace perfectforwarding
{
template<class T, class U>
constexpr auto maximum(T&& a, U&& b) -> decltype( (a > b) ? std::forward(a) : std::forward(b))
{
return (a > b) ? std::forward(a) : std::forward(b);
}
}
int main(int argc, const char * argv[])
{
std::cout << "Created count is: " << created_count << std::endl;
auto const result = perfectforwarding::maximum(5,6.0);
std::cout << "The maximum of 5 and 6: " << result << std::endl;
return 0;
}
Blair
std::forwardis a template, and you need to provide the type argument explicitly if you want it to work properly. This is how yourmaximumfunction template should be rewritten:This is how the
std::forwardutility is defined:The expression
typename remove_reference<T>::typemakes this a non-deduced context, which explains why type deduction fails if you do not explicitly provide the type argumentT.