我得到一个SFINAE错误,下面的代码,因为它不会使用模板。我试图完善前进的结果。任何想法任何人。完美转发不拾取方法
#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;
}
布莱尔
我不认为你会从那段代码中得到你想要的。 “decltype”内部的表达式是对三元运算符的评估,而三元运算符产生的表达式是两种替代方法的*常见类型*。基本上你的模板和'template auto maximum(T && x,T && y) - > std :: common_type(std :: forward(x),std :: forward(y))'一样。也就是说,它不会**选择值大于*的参数的类型。 –
2013-02-11 14:11:59