...
首页> 外文期刊>C - C++ Users Journal >Constrained Value Types Using Policies
【24h】

Constrained Value Types Using Policies

机译:使用策略的约束值类型

获取原文
获取原文并翻译 | 示例
           

摘要

A common scenario that occurs frequenfly in programming is that of enforcing constraints on a value type, such as to be within a valid range. This is one of those cases where, typically, programmers end up reinventing the wheel each time because a truly general-purpose and reusable solution is not well known. Policy-driven design easily provides a solution to this problem, as it does many others where customizable type behavior is desirable. In this article, I present a general-purpose constrained value type (see Listings 1 and 2) that can be easily modified with policies to adapt to your specific needs. A few examples of possible candidates for a cpnstrained value type include: days of the week, minutes, seconds, hours, days of the year, months, degrees of a circle, radians, strictly positive values, nonnegative values, probability, percentages, and so on. For example, here I define an integer value type that represents a base 60 number and throws an exception if ever assigned a value out of the range [0..59].
机译:编程中经常出现的常见情况是对值类型强制执行约束,例如在有效范围内。在这种情况下,通常程序员每次都要重新发明轮子,因为真正通用和可重用的解决方案并不为人所知。策略驱动的设计很容易提供此问题的解决方案,就像其他许多需要可自定义类型行为的设计一样。在本文中,我提出了一种通用约束值类型(请参见清单1和2),可以使用策略轻松地对其进行修改以适应您的特定需求。 cpnstrained值类型的可能候选项的一些示例包括:星期几,分钟,秒,小时,一年中的天,月份,圆度,弧度,严格正值,非负值,概率,百分比和以此类推。例如,在这里我定义了一个整数类型,该类型表示以60为基数的数字,并且如果分配的值超出[0..59]范围,则​​抛出异常。

著录项

相似文献

  • 外文文献
  • 中文文献
  • 专利