Topic: Looking at STL code (reserved names)


Author: David R Tribble <david@tribble.com>
Date: 1999/08/03
Raw View
Biju Thomas wrote:
> A question - if I #define out normal names like const_iterator,
> value_type etc., and, include the standard library headers, is the
> resulting compiler errors my problem or a library problem?
>
> For example:
>
>   #define const_iterator
>   #define value_type
>
>   #include <set> // Or any container class from standard library
>
>   //... anything else.
>
> This code will give a huge list of compiler errors.
>
> Does the standard prohibit such '#define's?

Yes.

  17.1.17  reserved function

  a function, specified as part of the C++ Standard Library, that
  must be defined by the implementation.  If a C++ program provides
  a definition for any reserved function, the results are undefined.

  17.4.1.1  Library contents

  The C++ Standard Library provides definitions for the following
  types of entities: Macros, Values, Types, Templates, Classes,
  Functions, Objects.

  All library entities except macros, operator new and operator
  delete are defined within the namespace std or namespaces nested
  within namespace std.

  17.4.3.1  Reserved names

  It is undefined for a C++ program to add declarations or
  definitions to namespace std or namespaces within namespace std
  unless otherwise specified.  ...

  The C++ Standard Library reserves the following kinds of names:

  - Macros

  - Global names

  - Names with external linkage

  If the program declares or defines a name in a context where it is
  reserved, other than as explicitly allowed by this clause, the
  behavior is undefined.

  17.4.3.1.1  Macro names

  ...

  A translation unit that includes a header shall not contain any
  macros that define names declared or defined in that header.  Nor
  shall such a translation unit define macros for names lexically
  identical to keywords.

-- David R. Tribble, david@tribble.com --


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]