Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在任意延长的集合中查找最大数是';行不通_C++_Templates_C++11 - Fatal编程技术网

C++ 在任意延长的集合中查找最大数是';行不通

C++ 在任意延长的集合中查找最大数是';行不通,c++,templates,c++11,C++,Templates,C++11,我有一段代码,其中我试图从传递的参数中获取最大数量的参数。由于某种原因,它不起作用,我也不确定为什么。当我输入2个数字时,代码工作,但当传递3个或更多数字时,我会出现以下错误: prog.cpp:在函数“int main()”中: 程序cpp:31:29:错误:调用“max(int,int,int)”时没有匹配函数 项目cpp:31:29:注:候选人为: 程序cpp:24:30:注意:模板constexpr decltype(handle::helper::max(max::args…)max(

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

prog.cpp:在函数“int main()”中:
程序cpp:31:29:错误:调用“max(int,int,int)”时没有匹配函数
项目cpp:31:29:注:候选人为:
程序cpp:24:30:注意:模板constexpr decltype(handle::helper::max(max::args…)max(args…) 程序cpp:24:30:注意:模板参数扣除/替换失败:
prog.cpp:替换“模板constexpr decltype(handle::helper::max(args…)max(args…[带args={int,int,int}]”:
程序cpp:31:29:此处需要
prog.cpp:24:30:错误:调用“handle::helper::max(int&,int&,int&)”时没有匹配的函数
项目cpp:24:30:注:候选人为: prog.cpp:11:18:注意:静态T句柄::helper::max(T,T)[with T=int;Args={int,int}]
prog.cpp:11:18:注:候选人需要2个参数,提供3个参数
prog.cpp:16:18:注意:静态T句柄::helper::max(T,T,Args…[with T=int;Args={int,int}]
prog.cpp:16:18:注:候选人需要4个参数,提供3个参数

节目如下:

#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
}
#包括
名称空间句柄
{
模板
结构助手{};
模板
结构辅助程序
{
静态T constexpr max(T x,T y)
{
返回x>y?x:y;
}
静态T constexpr max(T x,T y,Args…Args)
{
返回最大值(x,最大值(y,参数…);
}
};
}
模板
静态自动constexpr max(Args…Args)->decltype(handle::helper::max(Args…)
{
返回句柄::helper::max(args…);
}
int main()
{

std::cout一个问题是参数错误。您正在调用
helper::max
。在模板中:

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

    constexpr static T max(T x, T y, Args... args)
    {
        return max(x, max(y, args...));
    }
};
因为现在您的函数变得与
max(x,y)
不明确

您可以做的是提供如下专门化:

template <typename T>
struct helper<T> {
  static T max(T x) { return x; }
};

template <typename X,typename Y,typename... Args>
struct helper<X,Y,Args...> {
  constexpr static X max(X x,Y y,Args... args)
  {
    return std::max(x,helper<Y,Args...>::max(y,args...));
  }
};
模板
结构辅助程序{
静态T max(tx){return x;}
};
模板
结构辅助程序{
constexpr静态X最大值(X X,Y,Args…Args)
{
返回std::max(x,helper::max(y,args…);
}
};

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

你可以妥协,改用这个

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...));
}

为什么你的函数是在一个结构中,这只会让事情变得更加混乱,我相信函数应该是模板而不是结构为什么不使用
std::max(std::initializer\u list)
?@aaronman我不能部分专门化函数,所以我需要使用一个类模板。你说不能部分专门化函数是什么意思,而且因为在你的函数中所有函数都是相同的类型,所以真的不需要变量templates@KennyTM为什么不使用std::set,为什么不使用一些lib,等等?Op对此很感兴趣为什么要使用
std::max
这里?@templateboy:似乎比创建另一个函数来做同样的事情更简单,但您当然可以编写自己的max函数并使用它。只需确保给它一个不同的名称,这样您就不会遇到名称隐藏问题。如果您替换
typename std::common\u type::type使用
typename std::common_type::type
@0x499602D2实际上更好。我认为问题在于gcc。我将根据您的建议进行编辑。@很快看到编辑。我认为这应该可以工作。如果选择了正确的类型。嗯,这实际上是一个奇怪的错误。我也想过使用
std::common_type
,但从没想过会这样做解决问题:o
template <typename T>
struct helper<T> {
  static T max(T x) { return x; }
};

template <typename X,typename Y,typename... Args>
struct helper<X,Y,Args...> {
  constexpr static X max(X x,Y y,Args... args)
  {
    return std::max(x,helper<Y,Args...>::max(y,args...));
  }
};
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); 
}
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...));
}
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...));
}
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...));
}