参数包后的默认参数
Defaulted parameter after parameter pack
这是标准 std::max
函数的扩展,因此它可以接受随机数量的参数。
template<typename T, typename U, typename Compare = std::greater<>> constexpr
const auto max(const T& first, const U& second, Compare comp = Compare())
{
return comp(first, second) ? first : second;
}
template<typename T, typename U, typename... Pack, typename Compare = std::greater<>> constexpr
const auto max(const T& first, const U& second, const Pack&... rest, Compare comp = Compare())
{
return comp(first, second) ? max(first, rest..., comp) : max(second, rest..., comp);
}
据我了解,comp
参数将采用我调用该函数所用的最后一个参数的值。
我错了吗?我该怎么办?
std::cout << max(2, 3, 4, 5); //boom
std::cout << max(2, 3, 4, 5, std::greater<>()); //boom
当然,如果我完全删除 comp
就可以了。
rest
的类型是非推导上下文,因此编译失败,因为未找到调用 max
的匹配函数(有关背景信息,请参阅 What is a non-deduced context? ):
example.cpp:18:18: error: no matching function for call to 'max'
std::cout << max(2, 3, 4, 5); //boom
^~~
example.cpp:5:12: note: candidate function template not viable: requires at most 3 arguments, but 4
were provided
const auto max(const T& first, const U& second, Compare comp = Compare())
^
example.cpp:11:12: note: candidate function not viable: requires at most 3 arguments, but 4 were
provided
const auto max(const T& first, const U& second, const Pack&... rest, Compare comp = Compare())
没有简单的解决方案可以避免这种情况。您可以考虑以下解决方法之一:
- 将
Compare
参数作为第一个参数传递
- 使用
initializer_list
传递参数(因此,所有参数都具有相同的类型)
在C++14标准中,他们选择了第二个选项:
template< class T, class Compare >
T max( std::initializer_list<T> ilist, Compare comp )
{
return *std::max_element(ilist.begin(), ilist.end(), comp);
}
这是标准 std::max
函数的扩展,因此它可以接受随机数量的参数。
template<typename T, typename U, typename Compare = std::greater<>> constexpr
const auto max(const T& first, const U& second, Compare comp = Compare())
{
return comp(first, second) ? first : second;
}
template<typename T, typename U, typename... Pack, typename Compare = std::greater<>> constexpr
const auto max(const T& first, const U& second, const Pack&... rest, Compare comp = Compare())
{
return comp(first, second) ? max(first, rest..., comp) : max(second, rest..., comp);
}
据我了解,comp
参数将采用我调用该函数所用的最后一个参数的值。
我错了吗?我该怎么办?
std::cout << max(2, 3, 4, 5); //boom
std::cout << max(2, 3, 4, 5, std::greater<>()); //boom
当然,如果我完全删除 comp
就可以了。
rest
的类型是非推导上下文,因此编译失败,因为未找到调用 max
的匹配函数(有关背景信息,请参阅 What is a non-deduced context? ):
example.cpp:18:18: error: no matching function for call to 'max'
std::cout << max(2, 3, 4, 5); //boom
^~~
example.cpp:5:12: note: candidate function template not viable: requires at most 3 arguments, but 4
were provided
const auto max(const T& first, const U& second, Compare comp = Compare())
^
example.cpp:11:12: note: candidate function not viable: requires at most 3 arguments, but 4 were
provided
const auto max(const T& first, const U& second, const Pack&... rest, Compare comp = Compare())
没有简单的解决方案可以避免这种情况。您可以考虑以下解决方法之一:
- 将
Compare
参数作为第一个参数传递 - 使用
initializer_list
传递参数(因此,所有参数都具有相同的类型)
在C++14标准中,他们选择了第二个选项:
template< class T, class Compare >
T max( std::initializer_list<T> ilist, Compare comp )
{
return *std::max_element(ilist.begin(), ilist.end(), comp);
}