EXPLORATION 16

image

Type Synonyms

Using types such as std::vector<std::string>::size_type or std::map<std::string, int>::iterator can be clumsy, prone to typographical errors, and just plain annoying to type and read. Fortunately, C++ lets you define short synonyms for clumsy types. You can also use type synonyms to provide meaningful names for generic types. (The standard library has quite a few synonyms of the latter variety.) These synonyms are often referred to as typedefs, because the keyword you use to declare them is typedef.

typedef Declarations

C++ inherits the basic syntax and semantics of typedef from C, so you might already be familiar with this keyword. If so, please bear with me while I bring other readers up to speed.

The idea of a typedef is to create a synonym, or alias, for another type. There are two compelling reasons for creating type synonyms.

  • They create a short synonym for a long type name. For example, you may want to use count_iter as a type synonym for std::map<std::string,int>::iterator.
  • They create a mnemonic synonym. For example, a program might declare height as a synonym for int, to emphasize that variables of type height store a height value. This information helps the human reader understand the program.

The basic syntax for a typedef declaration is like defining a variable, except you start with the typedef keyword, and the name of the type synonym takes the place of the variable name.

typedef std::map<std::string,int>::iterator count_iter;
typedef int height;

Revisit Listing 15-4 and simplify the program by using a typedef declaration. Compare your result with Listing 16-1.

Listing 16-1.  Counting Words, with a Clean Program That Uses typedef

#include <iomanip>
#include <iostream>
#include <map>
#include <string>
 
int main()
{
  std::map<std::string, int> counts{};
  typedef std::map<std::string,int> count_map;
  typedef count_map::iterator count_iterator;
 
  // Read words from the standard input and count the number of times
  // each word occurs.
  std::string word{};
  while (std::cin >> word)
    ++counts[word];
 
  count_iterator the{counts.find("the")};
 
  if (the == counts.end())
    std::cout << ""the": not found ";
  else if (the->second == 1)
    std::cout << ""the": occurs " << the->second << " time ";
  else
    std::cout << ""the": occurs " << the->second << " times ";
}

I like the new version of this program. It’s a small difference in this little program, but it lets us continue to use universal initialization, while preserving clarity and readability.

Common typedefs

The standard library makes heavy use of typedefs, as you have already seen. For example, std::vector<int>::size_type is a typedef for an integer type. You don’t know which integer type (C++ has several, which you will learn about in Exploration 25), nor does it matter. All you have to know is that size_type is the type to use if you want to store a size or index in a variable.

Most likely, size_type is a typedef for std::size_t, which is itself a typedef. The std::size_t typedef is a synonym for an integer type that is suitable for representing a size. In particular, C++ has an operator, sizeof, which returns the size in bytes of a type or object. The result of sizeof is an integer of type std::size_t, however a compiler-writer chooses to implement sizeof and std::size_t.

image Note  A “byte” is defined as the size of type char. So, by definition, sizeof(char) == 1. The size of other types depends on the implementation. On most popular desktop workstations, sizeof(int) == 4, but 2 and 8 are also likely candidates.

Type Aliases

Another way to declare a typedef is with the using keyword. This style of type declaration is called a type alias. For example:

using height = int;
using count_map = std::map<std::string, int>;

The meaning of a type alias is identical to a typedef. They are merely two different syntaxes for the same concept. Some people find type aliases to be easier to read, because the name you are declaring comes first, rather than being hidden in a typedef declaration.

That was short and sweet, wasn’t it? Now you can return to the problem of counting words. This program has a number of usability flaws.

What can you think of to improve the word-counting program?

_____________________________________________________________

_____________________________________________________________

_____________________________________________________________

At the top of my list are the following two items:

  • Ignore punctuation marks.
  • Ignore case differences.

In order to implement these additional features, you have to learn some more C++. For example, the C++ standard library has functions to test whether a character is punctuation, a digit, an uppercase letter, a lowercase letter, and so on. The next Exploration begins by exploring characters more closely.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset