gpt4 book ai didi

c++ - 在任意加长的集合中查找最大数量是行不通的

转载 作者:太空狗 更新时间:2023-10-29 23:52:17 25 4
gpt4 key购买 nike

我有这段代码,其中我试图从传递的参数中获取最大数量。由于某种原因,它不起作用,我不确定为什么。当我输入 2 个数字时代码有效,但当输入 3 个或更多数字时,我收到以下错误:

prog.cpp: In function ‘int main()’:
prog.cpp:31:29: error: no matching function for call to ‘max(int, int, int)’
prog.cpp:31:29: note: candidate is:
prog.cpp:24:30: note: template constexpr decltype (handle::helper::max(max::args ...)) max(Args ...)
prog.cpp:24:30: note: template argument deduction/substitution failed:
prog.cpp: In substitution of ‘template constexpr decltype (handle::helper::max(args ...)) max(Args ...) [with Args = {int, int, int}]’:
prog.cpp:31:29: required from here
prog.cpp:24:30: error: no matching function for call to ‘handle::helper::max(int&, int&, int&)’
prog.cpp:24:30: note: candidates are: prog.cpp:11:18: note: static T handle::helper::max(T, T) [with T = int; Args = {int, int}]
prog.cpp:11:18: note: candidate expects 2 arguments, 3 provided
prog.cpp:16:18: note: static T handle::helper::max(T, T, Args ...) [with T = int; Args = {int, int}]
prog.cpp:16:18: note: candidate expects 4 arguments, 3 provided

程序如下:

#include <iostream>

namespace handle
{
template <typename... Args>
struct helper {};

template <typename T, typename... Args>
struct helper<T, Args...>
{
static T constexpr max(T x, T y)
{
return x > y ? x : y;
}

static T constexpr max(T x, T y, Args... args)
{
return max(x, max(y, args...));
}
};
}

template <typename... Args>
static auto constexpr max(Args... args) -> decltype(handle::helper<Args...>::max(args...))
{
return handle::helper<Args...>::max(args...);
}

int main()
{
std::cout << max(5, 3, 7); // fails
}

我真的很困惑,因为我以为我记下了这个。我做错了什么,我该如何解决?谢谢。


更新:感谢 Named。由于此问题现已解决,因此结果如下:

#include <type_traits>
#include <iostream>

namespace handle
{
template <typename T, typename V>
static auto constexpr max(T const& x, V const& y)
-> typename std::common_type<T, V>::type
{
return x > y ? x : y;
}

template <typename T, typename V, typename... Args>
static auto constexpr max(T const& x, V const& y, Args const&... args)
-> typename std::common_type<T, typename std::common_type<V, Args...>::type>::type
{
return max(x, max(y, args...));
}
}

template <typename... Args>
static auto constexpr max(Args const&... args) -> decltype(handle::max<Args...>(args...))
{
return handle::max<Args...>(args...);
}

int main()
{
std::cout << max(5, 3, 7.8, 2, 4, 55); // 55
}

谢谢大家!

最佳答案

Morwenn 已经指出了你的问题。但是,您仍然可以将代码简化为这样。 (您确实可以在 C++11 中特化函数模板)

namespace handle
{

template <typename T, typename V>
static auto max(const T& x, const V& y)
-> typename std::common_type<T,V>::type
{
return x > y ? x : y;
}

template <typename T, typename V, typename... Args>
static auto max(const T& x, const V& y, const Args&... args)
-> decltype( max(x, max(y, args...)) )
{
return max(x, max(y, args...));
}
}

int main()
{
std::cout << handle::max(1,2,3.3);
}

当您比较不同的类型时,这样做的好处是可以返回正确的类型。


编辑:显然,当您有超过 3 个参数时,这将不起作用。问题似乎是 gcc 在评估时不考虑可变参数 max 本身

decltype(max(y, args...) 

你可以妥协并改用它

template <typename T, typename V, typename... Args>
static auto max(const T& x, const V& y, const Args&... args)
-> typename std::common_type<T, V>::type
{
return max(x, max(y, args...));
}

0x499602D2 在评论中建议的更好的方法

template <typename T, typename V, typename... Args>
static auto max(const T& x, const V& y, const Args&... args)
-> typename std::common_type<T, V, Args...>::type
{
return max(x, max(y, args...));
}


编辑:使用 gcc-std=c++1y 标志(允许您发出尾随返回类型)您可以这样做。 (上面的情况肯定是一个错误)

template <typename T, typename V>
auto max(const T& x, const V& y) {
return x > y ? x : y;
}

template <typename T, typename V, typename... Args>
auto max(const T& x, const V& y, const Args&... args) {
return max(x, max(y, args...));
}

关于c++ - 在任意加长的集合中查找最大数量是行不通的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16509625/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com