Nicolai M. Josuttis: solutions in time  The C++ Standard Library: New Issues

The C++ Standard Library - A Tutorial and Reference

New Issues, not mentioned in the book yet
Feb 26, 2001

This page contains issues that were not mentioned in the book The C++ Standard Library by Nicolai M. Josuttis yet but you should know about when using the C++ standard library. It contains recent clarification of the Standard, additional examples, etc. I appreciate any constructive feedback or additional hints for this page.


auto_ptr and auto_ptr_ref
I got some questions regarding the exact behavior of auto_ptr and the motivation of auto_ptr_ref. See some details that might help to understand this issue on this page.


toupper(), tolower(), and isspace() and STL algorithms

In the past, there were a lot of discussions whether the usage of function such as toupper() and isspace() as predicates for STL algorithms is valid. For example:

 std::string s;

 // lowercase all characters
 
std::transform (s.begin(), s.end(), // source
                 s.begin(),          // destination
                 std::tolower);      // operation

 // find first whitespace character
 
std::find_if (s.begin(), s.end(), std::isspace);

The problem is as follows: In these examples, the one argument version of tolower() and isspace(), which were inherited from C, are meant. Therefore they both may have C linkage; that is, they may be declared as extern "C" (however, according to a defect report the standard recommends that it should be declared with C++ linkage). And in general, we have no concept of deducing linkage in templates in case the argument is not used as a whole inside the template (that is, it is declared a plain type T). So, the question is whether templates such as transform() or find_if() use the template argument as a whole.

However, after some discussion the Standard Committee thinks that this code is correct as written. This is because if an argument is completely generic then linkage is passed along inside templates and thus an identifier with C linkage is considered. Note however that some compilers will still consider this as an error. For them, a global qualification such as ::tolower usually works.

In addition compilers might warn about an implicit arithmetic conversion from 'int' to 'char'. This is correct because tolower() is declared as int tolower(int). But you can ignore this warning.


iterator and const_iterator

It turned out that the standard does not guarantee that you can compare an iterator with a const iterator of the same container. And indeed, on two well-known standard library implementations the following fails to compile:

 bool check_equal (std::deque::iterator i, std::deque::const_iterator ci) {
    return i == ci;
 }

Funny enough, ci == i does work on these implementations.

The Standard Committee considers this as a defect. Thus, it should be possible to compare iterators with const iterators in either way.


Two new resources about locales are available:


Qualify size_t and ptrdiff_t
Don't forget to qualify size_t and ptrdiff_t with std::. However, not all compilers handle this correctly yet.


Additional example for a user-defined allocator
See this page for an example of a user-defined allocator.


Additional example for calling num_get() for locales
See this page for an example of calling num_get() for a locale.


Additional example for a user-defined manipulator with parameters
See this page for an example of a user-defined manipulator with parameters and its usage.


make_pair and string constants
It turned out that according to language rules make_pair doesn't work for string constants. Thus, something like

std::multimap<std::string,long> phonebook;
phonebook.insert(std::make_pair("nico",12345));

wouldn't compile on standard conforming compilers. This, of course, is not intended. The Standard Committee is working on a solution of this problem in the Standard (e.g. overloading make_pair with array arguments). See the C++ Standard Library Defect Report List, Issue 181 for details.

 

Home of the C++ Library book