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++;检查范围内值的常规方法_C++_Templates - Fatal编程技术网

C++ c++;检查范围内值的常规方法

C++ c++;检查范围内值的常规方法,c++,templates,C++,Templates,在数学中有不同的范围:它们可以是开的((a,b)),闭的([a,b]),左开的(a,b]),或右开的([a,b])。 我想用C++(不11)编写模板函数,可以很容易地管理这些情况。但是我对元编程和模板不太有信心。 我想要这样的东西: const int max = MAX, int min = MIN; int x = value; // check close range if ( is_in_range( x, min, max ) ) //... if ( is_in_range(

在数学中有不同的范围:它们可以是开的((a,b)),闭的([a,b]),左开的(a,b]),或右开的([a,b])。

我想用C++(不11)编写模板函数,可以很容易地管理这些情况。但是我对元编程和模板不太有信心。

我想要这样的东西:

const int max = MAX, int min = MIN;
int x = value;
// check close range
if ( is_in_range( x, min, max ) ) //...

if ( is_in_range( x, min, max, open) ) //...
if ( is_in_range( x, min, max, left_open) ) //...
if ( is_in_range( x, min, max, right_open) ) //...
有人有什么建议吗

编辑1

我尝试过这个,但无法编译

enum { range_open, range_close, range_left_open, range_right_open };

namespace detail {

    template < typename Type >
    inline bool check_open_range( const Type& x, const Type& max, const Type& min )
    {
        return ( min < x ) && ( x < max );
    }

    template < typename Type >
    inline bool check_close_range( const Type& x, const Type& max, const Type& min )
    {
        return ( min <= x ) && ( x <= max );
    }

    template < typename Type >
    inline bool check_left_open_range( const Type& x, const Type& max, const Type& min )
    {
        return ( min < x ) && ( x <= max );
    }

    template < typename Type >
    inline bool check_right_open_range( const Type& x, const Type& max, const Type& min )
    {
        return ( min <= x ) && ( x < max );
    }

 }

template < typename Type, int range_open >
inline bool check_range( const Type& x, const Type& max, const Type& min );

template < typename Type, range_open >
inline bool check_range( const Type& x, const Type& max, const Type& min )
{
    return detail::check_open_range( x, min, max );
}

template < typename Type, range_close >
inline bool check_range( const Type& x, const Type& max, const Type& min )
{
    return detail::check_close_range( x, min, max );
}

template < typename Type, check_left_open_range >
inline bool check_range( const Type& x, const Type& max, const Type& min )
{
    return detail::check_left_open_range( x, min, max );
}

template < typename Type, check_right_open_range >
inline bool check_range( const Type& x, const Type& max, const Type& min )
{
    return detail::check_right_open_range( x, min, max );
}
enum{range\u open,range\u close,range\u left\u open,range\u right\u open};
名称空间详细信息{
模板<类型名称类型>
内联布尔检查打开范围(常数类型和x、常数类型和最大值、常数类型和最小值)
{
返回(最小值
内联布尔检查关闭范围(常数类型和x、常数类型和最大值、常数类型和最小值)
{
返回(最小值)
内联布尔检查范围(常数类型和x、常数类型和最大值、常数类型和最小值)
{
返回详细信息:检查打开范围(x、最小值、最大值);
}
模板<类型名称类型,范围\u关闭>
内联布尔检查范围(常数类型和x、常数类型和最大值、常数类型和最小值)
{
返回详细信息::检查关闭范围(x、最小值、最大值);
}
模板<类型名称类型,选中\u左侧\u打开\u范围>
内联布尔检查范围(常数类型和x、常数类型和最大值、常数类型和最小值)
{
返回详细信息:检查左打开范围(x、最小值、最大值);
}
模板<类型名称类型,选中\u右侧\u打开\u范围>
内联布尔检查范围(常数类型和x、常数类型和最大值、常数类型和最小值)
{
返回详细信息:检查右打开范围(x、最小值、最大值);
}
但实际上,使用4个重载函数更简单

编辑2

名称空间详细信息{
模板<类型名称类型>
内联布尔检查打开范围(常数类型和x、常数类型和最大值、常数类型和最小值)
{
返回(最小值
内联布尔检查关闭范围(常数类型和x、常数类型和最大值、常数类型和最小值)
{
返回(最小值)
内联布尔检查范围(常数类型和x、常数类型和最大值、常数类型和最小值、常数范围和打开值)
{
返回详细信息:检查打开范围(x、最小值、最大值);
}
模板<类型名称类型>
内联布尔检查范围(常数类型和x、常数类型和最大值、常数类型和最小值、常数范围和闭合值)
{
返回详细信息::检查关闭范围(x、最小值、最大值);
}
模板<类型名称类型>
内联布尔检查范围(常数类型和x、常数类型和最大值、常数类型和最小值、常数左侧打开范围和)
{
返回详细信息:检查左打开范围(x、最小值、最大值);
}
模板<类型名称类型>
内联布尔检查范围(常数类型和x、常数类型和最大值、常数类型和最小值、常数右侧打开范围和)
{
返回详细信息:检查右打开范围(x、最小值、最大值);
}
我认为编辑2是正确的

编辑3:好版本

结构左\u打开{ 样板 静态布尔比较(常量T和值,常量T和范围){返回范围<值;} })

struct left\u关闭{
样板

静态BoOL比较(cont t&值,const t &范围){返回范围您可以考虑模板编程的标签调度技术。您的iSynIn范围函数将有4个重载,每个重载都由一个参数(在函数中未使用)来区分。每一个超载都可以简单地增加/减少MI/MAX以达到你所追求的范围。

< P>你可以考虑模板编程的标签调度技术。iSynIn范围函数将有4个重载,每个重载都由一个参数(在函数中未使用)来区分。选择正确的重载。每个重载可以简单地增加/减少最小/最大值,以达到所需的范围。

一个简单的解决方案是使用重载:

namespace range_policy
{
  struct open {};
  struct left_open {};
  struct right_open {};
  ...
};

template <typename T>
bool is_in_range(const T& a, const T& min, const T& max, range_policy::open) { .... }

template <typename T>
bool is_in_range(const T& a, const T& min, const T& max, range_policy::left_open) { .... }

一个简单的解决方案是使用重载:

namespace range_policy
{
  struct open {};
  struct left_open {};
  struct right_open {};
  ...
};

template <typename T>
bool is_in_range(const T& a, const T& min, const T& max, range_policy::open) { .... }

template <typename T>
bool is_in_range(const T& a, const T& min, const T& max, range_policy::left_open) { .... }

为此进行枚举

enum RangeType
{
   close,
   open,
   left_open,
   right_open
}

bool is_in_range(int x, int min, int max, RangeType type) { }

为此进行枚举

enum RangeType
{
   close,
   open,
   left_open,
   right_open
}

bool is_in_range(int x, int min, int max, RangeType type) { }

以下是在VS2010上起作用的内容:-

class LeftOpen 
{
public:
  template <class T>
  static bool Compare (const T value, const T range) { return value >= range; }
};

class LeftClosed
{
public:
  template <class T>
  static bool Compare (const T value, const T range) { return value > range; }
};

class RightOpen 
{
public:
  template <class T>
  static bool Compare (const T value, const T range) { return value <= range; }
};

class RightClosed
{
public:
  template <class T>
  static bool Compare (const T value, const T range) { return value < range; }
};

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

int main()
{
  int
    min = 5,
    max = 99;

  bool
    r1 = IsInRange <LeftOpen, RightOpen> (-19, min, max),
    r2 = IsInRange <LeftOpen, RightOpen> (45, min, max),
    r3 = IsInRange <LeftOpen, RightOpen> (149, min, max);
}
类LeftOpen
{
公众:
样板
静态布尔比较(常量T值,常量T范围){返回值>=范围;}
};
类LeftClosed
{
公众:
样板
静态布尔比较(常量T值,常量T范围){返回值>范围;}
};
类RightOpen
{
公众:
样板

静态布尔比较(const T value,const T range){返回值这里有一些在VS2010上有效的东西:-

class LeftOpen 
{
public:
  template <class T>
  static bool Compare (const T value, const T range) { return value >= range; }
};

class LeftClosed
{
public:
  template <class T>
  static bool Compare (const T value, const T range) { return value > range; }
};

class RightOpen 
{
public:
  template <class T>
  static bool Compare (const T value, const T range) { return value <= range; }
};

class RightClosed
{
public:
  template <class T>
  static bool Compare (const T value, const T range) { return value < range; }
};

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

int main()
{
  int
    min = 5,
    max = 99;

  bool
    r1 = IsInRange <LeftOpen, RightOpen> (-19, min, max),
    r2 = IsInRange <LeftOpen, RightOpen> (45, min, max),
    r3 = IsInRange <LeftOpen, RightOpen> (149, min, max);
}
类LeftOpen
{
公众:
样板
静态布尔比较(常量T值,常量T范围){返回值>=范围;}
};
类LeftClosed
{
公众:
样板
静态布尔比较(常量T值,常量T范围){返回值>范围;}
};
类RightOpen
{
公众:
样板

静态bool比较(const T value,const T range){返回值您需要为此进行元编程吗?看起来您想要确定“开放度”(关闭/打开/左\开/右\开)这是有意的,还是您可以决定在编写代码时?在模板元编程中,通常使用
代替
()
(然后必须手动检索结果值)STL中是否有可用于检查范围的内容?您是否需要为此进行元编程?看起来您想要确定“开放度”(关闭/打开/左/右/打开)这是有意的,还是您可以决定在编写代码时?在模板元编程中,通常使用
代替
()
(然后必须手动检索结果值)在STL,是否有一些可用的方法来检查范围?他似乎希望所有类型的范围函数都通用,而不是仅仅是In。他似乎希望所有类型的范围函数都通用,而不仅仅是I.OK……我正在精确地搜索……但它是标准C++吗?它可以用G+或CLAN-E.VISU.DuKAJ编译:如果可以的话,它对任何事情都不是很“聪明”。唯一的问题可能是第三个模板参数的推导