1.4 Some Remarks About Programming Style

Ru-Brd

C++ programmers use different programming styles, and so do we: The usual questions about where to put whitespace, delimiters (braces, parentheses), and so forth came up. We tried to be consistent in general, although we occasionally make concessions to the topic at hand. For example, in tutorial sections we may prefer generous use of whitespace and concrete names to help visualize code, whereas in more advanced discussions a more compact style could be more appropriate.

We do want to draw your attention to one slightly uncommon decision regarding the declaration of types, parameters, and variables . Clearly, several styles are possible:

 void foo (const int &x);  void foo (const int& x);  void foo (int const &x);  void foo (int const& x); 

Although it is a bit less common, we decided to use the order int const rather than const int for "constant integer." We have two reasons for this. First, it provides for an easier answer to the question, " What is constant?" It's always what is in front of the const qualifier. Indeed, although

 const int N = 100; 

is equivalent to

 int const N = 100; 

there is no equivalent form for

 int* const bookmark;  // the pointer cannot change, but the   // value pointed to can change  

that would place the const qualifier before the pointer operator * . In this example, it is the pointer itself that is constant, not the int to which it points.

Our second reason has to do with a syntactical substitution principle that is very common when dealing with templates. Consider the following two type definitions [1] :

[1] Note that in C++ a type definition defines a "type alias" rather than a new type. For example:

 typedef int Length;  // define  Length  as an alias for  int  int i = 42;  Lengthl = 88;  i = l;  // OK  l = i;  // OK  
 typedef char* CHARS;  typedef CHARS const CPTR;  // constant pointer to  char  s  

The meaning of the second declaration is preserved when we textually replace CHARS with what it stands for:

 typedef char* const CPTR;  // constant pointer to  char  s  

However, if we write const before the type it qualifies, this principle doesn't apply. Indeed, consider the alternative to our first two type definitions presented earlier:

 typedef char* CHARS;  typedef const CHARS CPTR;  // constant pointer to  char  s  

Textually replacing CHARS results in a type with a different meaning:

 typedef const char* CPTR;  // pointer to constant  char  s  

The same observation applies to the volatile specifier , of course.

Regarding whitespaces, we decided to put the space between the ampersand and the parameter name :

 void foo (int const& x); 

By doing this, we emphasize the separation between the parameter type and the parameter name. This is admittedly more confusing for declarations such as

 char* a, b; 

where, according to the rules inherited from C, a is a pointer but b is an ordinary char . To avoid such confusion, we simply avoid declaring multiple entities in this way.

This is not a book about the C++ standard library, but we do make use of that library in some of our examples. In general, we use the C++-specific headers (for example, < iostream > rather than <stdio.h> ). The exception is <stddef.h> . We use it instead of <cstddef> and therefore do not qualify size_t and ptrdiff_t with the std:: prefix because this is still more portable and there is no advantage in using std::size_t instead of size_t .

Ru-Brd


C++ Templates
C++ Templates: The Complete Guide
ISBN: 0201734842
EAN: 2147483647
Year: 2002
Pages: 185

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net