Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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++ 将typedef与模板一起使用_C++_Templates - Fatal编程技术网

C++ 将typedef与模板一起使用

C++ 将typedef与模板一起使用,c++,templates,C++,Templates,下面的代码和修改的位使用g++编译器生成以下错误消息: 错误:“typedef”的模板声明 /RangeChecks.hpp:145:12:错误:“IsInRange”未命名类型 以下是my RangeChecks.hpp文件中的相关部分: class GreaterEqual { public: template <class T> static bool Compare (const T& value, const T& threshold

下面的代码和修改的位使用g++编译器生成以下错误消息: 错误:“typedef”的模板声明 /RangeChecks.hpp:145:12:错误:“IsInRange”未命名类型

以下是my RangeChecks.hpp文件中的相关部分:

class GreaterEqual
{
  public:
     template <class T>
     static bool Compare (const T& value, const T& threshold)
     {
        return !(value < threshold); /* value >= threshold */
     }
};

class LessEqual
{
  public:
     template <class T>
     static bool Compare (const T& value, const T& threshold)
     {
        return !(value > threshold); /* value <= threshold */
     }
};

template <class L, class R, class T>
bool IsInRange (const T& value, const T& min, const T& max)
{
     return L::template Compare<T> (value, min) && R::template Compare<T> (value, max);
}

typedef IsInRange< GreaterEqual , LessEqual > isInClosedRange;
我在互联网上搜索了一个答案,找到了一些类似的东西,但没有一个,解决了我的问题。

IsInRange是一个函数模板,而不是类模板,所以它的实例化不是类型,所以你不能为它创建typedef。

IsInRange是一个函数,而不是类型。最简单的方法是编写一个包装器:

template<class T>
bool isInClosedRange(const T& value, const T& min, const T& max) {
    return IsInRange<T, GreaterEqual, LessEqual>(value, min, max);
}

@myaut这些类型的别名声明仍然只适用于类型,这是一个函数。是否可以将IsInRange定义为类模板?这样做有意义吗?