According to the C++ standard(N4296),
§ 21.5
21.5 Numeric conversions [string.conversions]
- Effects: the first two functions call
strtol(str.c_str(), ptr, base)
, and the last three functions
callstrtoul(str.c_str(), ptr, base)
,strtoll(str.c_str(), ptr, base)
, andstrtoull(
, respectively. Each function returns the converted result, if any. The argument
str.c_str(), ptr, base)
ptr designates a pointer to an object internal to the function that is used to determine what
to store at*idx
. If the function does not throw an exception andidx != 0
, the function stores in
*idx
the index of the first unconverted element of str.- Returns: The converted result.
- Throws:
invalid_argument
ifstrtol
,strtoul
,strtoll
, orstrtoull
reports that no conversion
could be performed. Throwsout_of_range
ifstrtol
,strtoul
,strtoll
orstrtoull
setserrno
to
ERANGE
, or if the converted value is outside the range of representable values for the return type.
What is strtol
?
§ 21.8
21.8 Null-terminated sequence utilities [c.strings]
- Tables 74, 75, 76, 77, 78, and 79 describe headers
<cctype>
,<cwctype>
,<cstring>
,<cwchar>
,<cstdlib>
(character conversions), and<cuchar>
, respectively.- The contents of these headers shall be the same as the Standard C Library headers
<ctype.h>
,<wctype.h>
,
<string.h>
,<wchar.h>
, and<stdlib.h>
and the C Unicode TR header<uchar.h>
, respectively, with the
following modifications:Table 78 — Header
<cstdlib>
synopsisType Name(s)
Macros: MB_CUR_MAX
Functions:
atof mblen strtof strtoul
atoi mbtowc strtol strtoull
atol mbstowcs strtold wctomb
atoll strtod strtoll wcstombs
What the meaning of Standard C Library
?
§ 1.2
1.2 Normative references [intro.refs]
The following referenced documents are indispensable for the application of this document. For dated references,
only the edition cited applies. For undated references, the latest edition of the referenced document
(including any amendments) applies.(1.3) — ISO/IEC 9899:1999, Programming languages — C
(1.4) — ISO/IEC 9899:1999/Cor.1:2001(E), Programming languages — C, Technical Corrigendum 1
(1.5) — ISO/IEC 9899:1999/Cor.2:2004(E), Programming languages — C, Technical Corrigendum 2The library described in Clause 7 of ISO/IEC 9899:1999 and Clause 7 of ISO/IEC 9899:1999/Cor.1:2001
and Clause 7 of ISO/IEC 9899:1999/Cor.2:2003 is hereinafter called the C standard library.1
So, we need to watch C99 standard.
According to the C99 standard(N1124)
§7.20.1.4
7.20.1.4 The strtol, strtoll, strtoul, and strtoull functions
- Description
Thestrtol
,strtoll
,strtoul
, andstrtoull
functions convert the initial portion of the string pointed to bynptr
tolong int
,long long int
,unsigned long int
, andunsigned long long int
representation, respectively. First, they decompose the input string into three parts: an initial, possibly empty, sequence of
white-space characters (as specified by theisspace
function), a subject sequence resembling an integer represented in some radix determined by the value ofbase
, and a final string of one or more unrecognized characters, including the terminating null character of the input string. Then, they attempt to convert the subject sequence to an integer, and return the result.- If the value of
base
is zero, the expected form of the subject sequence is that of an integer constant as described in 6.4.4.1, optionally preceded by a plus or minus sign, but not including an integer suffix. If the value ofbase
is between 2 and 36 (inclusive), the expected form of the subject sequence is a sequence of letters and digits representing an integer with the radix specified bybase
, optionally preceded by a plus or minus sign,
but not including an integer suffix. The letters froma
(orA
) throughz
(orZ
) are ascribed the values 10 through 35; only letters and digits whose ascribed values are less than that ofbase
are permitted. If the value ofbase
is 16, the characters0x
or0X
may optionally precede the sequence of letters and digits, following the sign if present.- The subject sequence is defined as the longest initial subsequence of the input string, starting with the first non-white-space character, that is of the expected form. The subject sequence contains no characters if the input string is empty or consists entirely of white space, or if the first non-white-space character is other than a sign or a permissible letter
or digit.- If the subject sequence has the expected form and the value of
base
is zero, the sequence of characters starting with the first digit is interpreted as an integer constant according to the rules of 6.4.4.1. If the subject sequence has the expected form and the value ofbase
is between 2 and 36, it is used as the base for conversion, ascribing to each letter its value as given above. If the subject sequence begins with a minus sign, the value resulting from the conversion is negated (in the return type). A pointer to the final string is stored in the object pointed to byendptr
, provided thatendptr
is not a null pointer.- In other than the
"C"
locale, additional locale-specific subject sequence forms may be accepted.- If the subject sequence is empty or does not have the expected form, no conversion is performed; the value of
nptr
is stored in the object pointed to byendptr
, provided thatendptr
is not a null pointer.- Returns
Thestrtol
,strtoll
,strtoul
, andstrtoull
functions return the converted value, if any. If no conversion could be performed, zero is returned. If the correct value is outside the range of representable values,LONG_MIN
,LONG_MAX
,LLONG_MIN
,LLONG_MAX
,ULONG_MAX
, orULLONG_MAX
is returned (according to the return type and sign of the value, if any), and the value of the macroERANGE
is stored inerrno
.
Now we need to watch C++ standard again.
21.5 Numeric conversions [string.conversions]
- Throws:
invalid_argument
ifstrtol
,strtoul
,strtoll
, orstrtoull
reports that no conversion
could be performed. Throwsout_of_range
ifstrtol
,strtoul
,strtoll
orstrtoull
setserrno
to
ERANGE
, or if the converted value is outside the range of representable values for the return type.
Now you can understand when invalid_argument
and out_of_range
exception is thrown, I hope.