Channels ▼
RSS

Security

Ada-style Ranged Types in C++


The Interface

The ranged type class template is defined approximately as:

template<class T, T MIN, T MAX, class TR 
    = ranged_traits<T, MIN, MAX> >
class ranged_type;

Where T is the base type, MIN and MAX define the range, and TR defines a traits class. The traits class defines various members to control the behavior of various aspects of the class, such as what is to be done upon detecting an overflow condition. The default of which is to throw an exception.

I said the class template is approximately defined as above because there are additional template parameters that are defaulted. These are defined by the implementation and are used for class specialization. These parameters are defined from members of the traits class, and allow classes to be instantiated with or without explicit constructors taking primitive type arguments, and/or cast operators to primitive types. The default is the safest, having explicit constructors and no cast operator. Assignment, arithmetic, relational, and equality operators are defined for the class. These are defined for every combination of the ranged type class and its intermediate types. Intermediate types hold the result of arithmetic operations and keep track of the maximum number of digits.

Mixing constants and variables of primitive types in expressions with ranged types is problematic. At issue is determining at compile-time the number of digits in the value. Obviously, the number of digits in an integral constant expression (ICE) can be calculated at compile-time, but the C++ language does not allow an operator to be overloaded based on that. The very nature of a variable makes it impossible to determine its number of digits at compile-time. Casting the value to the ranged type is one way to use it in an expression. There are two possible problems with this: 1) it can be inefficient because it causes additional range checks to be done, and 2) the value may exceed the range. To compensate for this, two templates are defined, constant and variable, that convert values to the range type's intermediate type. Their use will lead to expressions that are harder to read, but can be more efficient to execute.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 

Video