Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

C/C++

Counting Array Elements at Compile Time


Compile-Time Assertions

Arrays are not just a convenience feature. Since their size is fixed at compile time, they can be used in compile-time checks that would not otherwise be possible. For example, suppose I have an enumeration, and I want to associate a string with each enumerated value. Listing Three works but is not very robust because it gives incorrect results if I later add a new value to the enum but forget to update the array. By making a couple of minor changes to the code (Listing Four), I can make the compiler double-check my work. COMPILE_TIME_ASSERT is a macro that takes a compile-time constant as its argument. (For an implementation, see the complete source code for this article; refer to "Resource Center," page 5.) If the argument is True, the macro has no effect. If it is False, the macro produces a compile-time error. In Listing Four, the assertion is that the enum and the array have the same number of elements. Therefore, if the enum and array should ever get out of sync, the compiler lets me know about it.

// in Color.h
enum Color
{
   red, blue, green,
};
// in Color.cpp
const char* color_names[] = { "red", "blue", "green" };
const char* get_name(Color c)
{
   return color_names[c];
}

Listing Three

// in Color.h
enum Color
{
   red, blue, green,
   last_color  // one-past-the-end value; must be last
};
// in Color.cpp
const char* color_names[] =
   { "red", "blue", "green", "invalid color" };
const char* get_name(Color c)
{
   return color_names[c];
}
COMPILE_TIME_ASSERT( COUNTOF(color_names) == last_color + 1 );

Listing Four

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.