Const-preserving overloads for the
strtox family of functions
||Programming Language C++, Library Evolution Working Group
||James Touton <email@example.com>
- Table of Contents
- Impact On the Standard
The C++ standard library adds const-preserving overloads for specific functions from the C library dealing with null-terminated strings.
As defined in the C standard, these functions accept
const char* parameters and return
char* values derived from those parameters.
Calling these functions would effectively result in silent casts from
const char* to
To prevent this, C++ replaces these C functions with const-preserving overloads;
for each such C library function, the C++ library provides a version accepting
const char* parameters and a version accepting
These functions have the same behavior as the C library version, but the return value preserves the type of the argument (
const char* or
This proposal adds the
strtox family of functions to the set of C library functions with const-preserving overloads.
strtox functions convert the initial portion of a string to a numeric value and fill a user-provided "out" parameter with a pointer to the next character in the string.
Although the "in" pointer has the type
const char*, the "out" parameter has the type
char**, throwing away the const qualifier.
This is pretty much asking for undefined behavior:
Example (current usage, generates no warnings):
const char* PI_STR = "3.14159 or so";
char* endptr; // can't use const char*, would fail to compile on the next line
float pi = strtof(PI_STR, &endptr); // bad: silent cast to char*
*endptr = 'x'; // universe explodes
The proposal adds a few new overloads covering the C library
strtox family of functions and deprecates the existing versions.
Apart from the deprecations, the wording is essentially identical to wording for other overloads already present.
All modifications are presented relative to N4567.
Insert the following new paragraphs in §21.8 [c.strings] after paragraph 13:
Add the following new section to Annex D [depr]: