forked from cplusplus/draft
/
conversions.tex
609 lines (525 loc) · 24.9 KB
/
conversions.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
%!TEX root = std.tex
\rSec0[conv]{Standard conversions}
\indextext{implicit conversion|see{conversion, implicit}}
\indextext{contextually converted to bool|see{conversion, contextual}}
\indextext{rvalue!lvalue conversion to|see{conversion, lvalue-to-rvalue}}%
\pnum
\indextext{conversion!standard|(}%
\indextext{conversion!implicit}%
Standard conversions are implicit conversions with built-in meaning.
\ref{conv} enumerates the full set of such conversions. A
\defnx{standard conversion sequence}{conversion sequence!standard} is a sequence of standard
conversions in the following order:
\begin{itemize}
\item Zero or one conversion from the following set: lvalue-to-rvalue
conversion, array-to-pointer conversion, and function-to-pointer
conversion.
\item Zero or one conversion from the following set: integral
promotions, floating-point promotion, integral conversions, floating-point
conversions, floating-integral conversions, pointer conversions,
pointer-to-member conversions, and boolean conversions.
\item Zero or one function pointer conversion.
\item Zero or one qualification conversion.
\end{itemize}
\begin{note}
A standard conversion sequence can be empty, i.e., it can consist of no
conversions. \end{note} A standard conversion sequence will be applied to
an expression if necessary to convert it to a required destination type.
\pnum
\begin{note}
Expressions with a given type will be implicitly converted to other
types in several contexts:
\begin{itemize}
\item When used as operands of operators. The operator's requirements
for its operands dictate the destination type\iref{expr.compound}.
\item When used in the condition of an \tcode{if} statement\iref{stmt.if} or
iteration statement\iref{stmt.iter}. The destination type is
\tcode{bool}.
\item When used in the expression of a \tcode{switch} statement\iref{stmt.switch}.
The destination type is integral.
\item When used as the source expression for an initialization (which
includes use as an argument in a function call and use as the expression
in a \tcode{return} statement). The type of the entity being initialized
is (generally) the destination type.
See~\ref{dcl.init}, \ref{dcl.init.ref}.
\end{itemize}
\end{note}
\pnum
An expression \tcode{e} can be
\defnx{implicitly converted}{conversion!implicit} to a type \tcode{T} if and only if the
declaration \tcode{T t=e;} is well-formed, for some invented temporary
variable \tcode{t}\iref{dcl.init}.
\pnum
Certain language constructs require that an expression be converted to a Boolean
value. An expression \tcode{e} appearing in such a context is said to be
\defnx{contextually converted to \tcode{bool}}{conversion!contextual to \tcode{bool}} and is well-formed if and only if
the declaration \tcode{bool t(e);} is well-formed, for some invented temporary
variable \tcode{t}\iref{dcl.init}.
\pnum
Certain language constructs require conversion to a value having
one of a specified set of types appropriate to the construct. An
expression \tcode{e} of class type \tcode{E} appearing in such a
context is said to be
\indextext{conversion!contextual}%
\defn{contextually implicitly converted} to a specified type \tcode{T} and is
well-formed if and only if \tcode{e} can be implicitly converted to a type \tcode{T}
that is determined as follows:
\tcode{E} is searched for non-explicit conversion functions
whose return type is \cv{} \tcode{T} or reference to \cv{}
\tcode{T} such that \tcode{T} is allowed by the context.
There shall be exactly one such \tcode{T}.
\pnum
The effect of any implicit
conversion is the same as performing the corresponding declaration and initialization
and then using the temporary variable as the result of the conversion.
The result is an lvalue if \tcode{T} is an lvalue reference
type or an rvalue reference to function type\iref{dcl.ref},
an xvalue if \tcode{T} is an rvalue reference to object type,
and a prvalue otherwise. The expression \tcode{e}
is used as a glvalue if and only if the initialization uses it as a glvalue.
\pnum
\begin{note}
For class types, user-defined conversions are considered as well;
see~\ref{class.conv}. In general, an \link{implicit conversion
sequence}{\ref{over.best.ics}} consists of a standard conversion
sequence followed by a user-defined conversion followed by another
standard conversion sequence.
\end{note}
\pnum
\begin{note}
There are some contexts where certain conversions are suppressed. For
example, the lvalue-to-rvalue conversion is not done on the operand of
the unary \tcode{\&} operator. Specific exceptions are given in the
descriptions of those operators and contexts.
\end{note}
\rSec1[conv.lval]{Lvalue-to-rvalue conversion}
\pnum
\indextext{conversion!lvalue-to-rvalue}%
\indextext{type!incomplete}%
A \deflink{glvalue}{\ref{basic.lval}} of a non-function, non-array type \tcode{T}
can be converted to
a prvalue.\footnote{For historical reasons, this conversion is called the ``lvalue-to-rvalue''
conversion, even though that name does not accurately reflect the taxonomy
of expressions described in~\ref{basic.lval}.}
If \tcode{T} is an incomplete type, a
program that necessitates this conversion is ill-formed. If \tcode{T}
is a non-class type, the type of the prvalue is
the cv-unqualified version of \tcode{T}. Otherwise, the type of the
prvalue is \tcode{T}.%
\footnote{In \Cpp{} class and array prvalues can have cv-qualified types.
This differs from ISO C, in which non-lvalues never have
cv-qualified types.}
\pnum
When an lvalue-to-rvalue conversion
is applied to an expression \tcode{e}, and either
\begin{itemize}
\item \tcode{e} is not potentially evaluated, or
\item the evaluation of \tcode{e} results in the evaluation of a member
\tcode{ex} of the set of potential results of \tcode{e}, and \tcode{ex}
names a variable \tcode{x} that is not \link{odr-used}{\ref{basic.def.odr}} by
\tcode{ex},
\end{itemize}
the value contained in the referenced object is not accessed.
\begin{example}
\begin{codeblock}
struct S { int n; };
auto f() {
S x { 1 };
constexpr S y { 2 };
return [&](bool b) { return (b ? y : x).n; };
}
auto g = f();
int m = g(false); // undefined behavior due to access of \tcode{x.n} outside its lifetime
int n = g(true); // OK, does not access \tcode{y.n}
\end{codeblock}
\end{example}
\pnum
The result of the conversion is determined according to the
following rules:
\begin{itemize}
\item If \tcode{T} is \cv{}~\tcode{std::nullptr_t}, the result is a
\deflink{null pointer constant}{\ref{conv.ptr}}.
\begin{note}
Since no value is fetched from memory,
there is no side effect for a volatile access\iref{intro.execution}, and
an inactive member of a \link{union}{\ref{class.union}} may be accessed.
\end{note}
\item Otherwise, if \tcode{T} has a class
type, the conversion copy-initializes the result object from
the glvalue.
\item Otherwise, if the object to which the glvalue refers contains an invalid
pointer value~(\ref{basic.stc.dynamic.deallocation},
\ref{basic.stc.dynamic.safety}), the behavior is
\impldef{lvalue-to-rvalue conversion of an invalid pointer value}.
\item Otherwise, the value contained in the object indicated by the
glvalue is the prvalue result.
\end{itemize}
\pnum
\begin{note}
See also~\ref{basic.lval}.\end{note}
\rSec1[conv.array]{Array-to-pointer conversion}
\pnum
\indextext{conversion!array-to-pointer}%
\indextext{decay!array|see{conversion, array-to-pointer}}%
\indextext{decay!function|see{conversion, function-to-pointer}}%
An lvalue or rvalue of type ``array of \tcode{N} \tcode{T}'' or ``array
of unknown bound of \tcode{T}'' can be converted to a prvalue of type
``pointer to \tcode{T}''.
The \link{temporary materialization conversion}{\ref{conv.rval}} is applied.
The result is a pointer to the first element of the array.
\rSec1[conv.func]{Function-to-pointer conversion}
\pnum
\indextext{conversion!function-to-pointer}%
An lvalue of function type \tcode{T} can be converted to a prvalue of
type ``pointer to \tcode{T}''. The result is a pointer to the
function.\footnote{This conversion never applies to non-static member functions because an
lvalue that refers to a non-static member function cannot be obtained.}
\pnum
\begin{note}
See~\ref{over.over} for additional rules for the case where the function
is overloaded.
\end{note}
\rSec1[conv.rval]{Temporary materialization conversion}
\indextext{conversion!temporary materialization}%
\pnum
A prvalue of type \tcode{T} can be converted to an xvalue of type \tcode{T}.
This conversion initializes a \link{temporary object}{\ref{class.temporary}} of type \tcode{T} from the prvalue
by evaluating the prvalue with the temporary object as its result object,
and produces an xvalue denoting the temporary object.
\tcode{T} shall be a complete type.
\begin{note}
If \tcode{T} is a class type (or array thereof),
it must have an accessible and non-deleted destructor;
see~\ref{class.dtor}.
\end{note}
\begin{example}
\begin{codeblock}
struct X { int n; };
int k = X().n; // OK, \tcode{X()} prvalue is converted to xvalue
\end{codeblock}
\end{example}
\rSec1[conv.qual]{Qualification conversions}
\indextext{conversion!qualification|(}%
\pnum
A \defn{cv-decomposition} of a type \tcode{T}
is a sequence of
$\cv{}_i$ and $P_i$
such that \tcode{T} is
\begin{indented}
``$\cv{}_0$ $P_0$ $\cv{}_1$ $P_1$ $\cdots$ $\cv{}_{n-1}$ $P_{n-1}$ $\cv{}_n$ \tcode{U}'' for $n > 0$,
\end{indented}
where
each $\cv{}_i$ is a set of cv-qualifiers\iref{basic.type.qualifier}, and
each $P_i$ is
\linkx{``pointer to''}{declarator!pointer}{\ref{dcl.ptr}},
\linkx{``pointer to member of class $C_i$ of type''}{declarator!pointer to member}{\ref{dcl.mptr}},
``array of $N_i$'', or
``\link{array of unknown bound}{\ref{dcl.array}} of''.
If $P_i$ designates an array,
the cv-qualifiers $\cv{}_{i+1}$ on the element type are also taken as
the cv-qualifiers $\cv{}_i$ of the array.
\begin{example}
The type denoted by the \grammarterm{type-id} \tcode{const int **}
has two cv-decompositions,
taking \tcode{U} as ``\tcode{int}'' and as ``pointer to \tcode{const int}''.
\end{example}
The $n$-tuple of cv-qualifiers after the first one
in the longest cv-decomposition of \tcode{T}, that is,
$\cv{}_1, \cv{}_2, \dotsc, \cv{}_n$, is called the
\defn{cv-qualification signature} of \tcode{T}.
\pnum
\indextext{type!similar|see{similar types}}%
Two types $\tcode{T}_1$ and $\tcode{T}_2$ are \defnx{similar}{similar types} if
they have cv-decompositions with the same $n$
such that corresponding $P_i$ components are the same
and the types denoted by \tcode{U} are the same.
\pnum
A prvalue expression of type $\tcode{T}_1$
can be converted to type $\tcode{T}_2$
if the following conditions are satisfied,
% NB: forbid line break between 'where' and 'cv'
% to stop superscript j from running into
% descender of p on the previous line.
where~$\cv{}_i^j$ denotes the cv-qualifiers in the cv-qualification signature of $\tcode{T}_j$:%
\footnote{These rules ensure that const-safety is preserved by the conversion.}
\begin{itemize}
\item $\tcode{T}_1$ and $\tcode{T}_2$ are similar.
\item For every $i > 0$, if \tcode{const} is in $\cv{}_i^1$ then \tcode{const} is in $\cv{}_i^2$, and similarly for \tcode{volatile}.
\item If the $\cv{}_i^1$ and $\cv{}_i^2$ are different,
then \tcode{const} is in every $\cv{}_k^2$ for $0 < k < i$.
\end{itemize}
\begin{note}
If a program could assign a pointer of type \tcode{T**} to a pointer of
type \tcode{const} \tcode{T**} (that is, if line \#1 below were
allowed), a program could inadvertently modify a const object
(as it is done on line \#2). For example,
\begin{codeblock}
int main() {
const char c = 'c';
char* pc;
const char** pcc = &pc; // \#1: not allowed
*pcc = &c;
*pc = 'C'; // \#2: modifies a const object
}
\end{codeblock}
\end{note}
\pnum
\begin{note}
A prvalue of type ``pointer to \cvqual{cv1} \tcode{T}'' can be
converted to a prvalue of type ``pointer to \cvqual{cv2} \tcode{T}'' if
``\cvqual{cv2} \tcode{T}'' is more cv-qualified than ``\cvqual{cv1}
\tcode{T}''.
A prvalue of type ``pointer to member of \tcode{X} of type \cvqual{cv1}
\tcode{T}'' can be converted to a prvalue of type ``pointer to member
of \tcode{X} of type \cvqual{cv2} \tcode{T}'' if ``\cvqual{cv2}
\tcode{T}'' is more cv-qualified than ``\cvqual{cv1} \tcode{T}''.
\end{note}
\pnum
\begin{note}
Function types (including those used in pointer to member function
types) are never cv-qualified\iref{dcl.fct}.
\end{note}
\indextext{conversion!qualification|)}
\rSec1[conv.prom]{Integral promotions}
\pnum
\indextext{promotion!integral}%
A prvalue of an integer type other than \tcode{bool}, \tcode{char16_t},
\tcode{char32_t}, or \tcode{wchar_t} whose \link{integer conversion
rank}{\ref{conv.rank}} is less than the rank of \tcode{int} can be
converted to a prvalue of type \tcode{int} if \tcode{int} can represent
all the values of the source type; otherwise, the source prvalue can be
converted to a prvalue of type \tcode{unsigned int}.
\pnum
\indextext{type!underlying!\idxcode{wchar_t}}%
\indextext{type!underlying!\idxcode{char16_t}}%
\indextext{type!underlying!\idxcode{char32_t}}%
A prvalue of type \tcode{char16_t}, \tcode{char32_t}, or
\tcode{wchar_t}\iref{basic.fundamental} can be converted to a prvalue
of the first of the following types that can represent all the values of
its underlying type: \tcode{int}, \tcode{unsigned int}, \tcode{long int},
\tcode{unsigned long int}, \tcode{long long int},
or \tcode{unsigned long long int}. If none of the types in that list can
represent all the values of its underlying type, a prvalue of type
\tcode{char16_t}, \tcode{char32_t}, or \tcode{wchar_t} can be converted
to a prvalue of its underlying type.
\pnum
\indextext{type!underlying!enumeration}%
A prvalue of an unscoped enumeration type whose underlying type is not
\deflinkx{fixed}{type!underlying!fixed}{\ref{dcl.enum}} can be converted to a prvalue of the first of the following
types that can represent all the values of the enumeration (i.e., the values in the
range $b_\text{min}$ to $b_\text{max}$ as described in~\ref{dcl.enum}): \tcode{int},
\tcode{unsigned int}, \tcode{long int}, \tcode{unsigned long int},
\tcode{long long int}, or \tcode{unsigned long long int}. If none of the types in that
list can represent all the values of the enumeration, a prvalue of an unscoped
enumeration type can be converted to a prvalue of the extended integer type with lowest
\link{integer conversion rank}{\ref{conv.rank}} greater than the rank of \tcode{long long}
in which all the values of the enumeration can be represented. If there are
two such extended types, the signed one is chosen.
\pnum
A prvalue of an unscoped enumeration type whose underlying type is
\deflinkx{fixed}{type!underlying!fixed}{\ref{dcl.enum}} can be converted to a prvalue of its underlying type. Moreover,
if integral promotion can be applied to its underlying type, a prvalue of an unscoped
enumeration type whose underlying type is fixed can also be converted to a prvalue of
the promoted underlying type.
\pnum
A prvalue for an integral \link{bit-field}{\ref{class.bit}} can be converted
to a prvalue of type \tcode{int} if \tcode{int} can represent all the
values of the bit-field; otherwise, it can be converted to
\tcode{unsigned int} if \tcode{unsigned int} can represent all the
values of the bit-field. If the bit-field is larger yet, no integral
promotion applies to it. If the bit-field has an enumerated type, it is
treated as any other value of that type for promotion purposes.
\pnum
\indextext{promotion!bool to int}%
A prvalue of type \tcode{bool} can be converted to a prvalue of type
\tcode{int}, with \tcode{false} becoming zero and \tcode{true} becoming
one.
\pnum
These conversions are called \defnx{integral promotions}{integral promotion}.
\rSec1[conv.fpprom]{Floating-point promotion}
\pnum
\indextext{promotion!floating-point}%
A prvalue of type \tcode{float} can be converted to a prvalue of type
\tcode{double}. The value is unchanged.
\pnum
This conversion is called \defn{floating-point promotion}.
\rSec1[conv.integral]{Integral conversions}
\pnum
\indextext{conversion!integral}%
A prvalue of an integer type can be converted to a prvalue of another
integer type. A prvalue of an unscoped enumeration type can be converted to
a prvalue of an integer type.
\pnum
\indextext{conversion!to unsigned}%
If the destination type is unsigned, the resulting value is the least
unsigned integer congruent to the source integer (modulo $2^n$ where $n$
is the number of bits used to represent the unsigned type).
\indextext{signed integer representation!two's complement}%
\begin{note}
In a two's complement representation, this conversion is conceptual and
there is no change in the bit pattern (if there is no truncation).
\end{note}
\pnum
\indextext{conversion!to signed}%
If the destination type is signed, the value is unchanged if it can be
represented in the destination type; otherwise,
the value is \impldef{value of result of unsigned to signed conversion}.
\pnum
\indextext{conversion!bool@\tcode{bool}}%
If the destination type is \tcode{bool}, see~\ref{conv.bool}. If the
source type is \tcode{bool}, the value \tcode{false} is converted to
zero and the value \tcode{true} is converted to one.
\pnum
The conversions allowed as integral promotions are excluded from the set
of integral conversions.
\rSec1[conv.double]{Floating-point conversions}
\pnum
\indextext{conversion!floating-point}%
A prvalue of floating-point type can be converted to a prvalue of
another floating-point type. If the source value can be exactly
represented in the destination type, the result of the conversion is
that exact representation. If the source value is between two adjacent
destination values, the result of the conversion is an
\impldef{result of inexact floating-point conversion} choice of either of those values.
Otherwise, the behavior is undefined.
\pnum
The conversions allowed as floating-point promotions are excluded from
the set of floating-point conversions.
\rSec1[conv.fpint]{Floating-integral conversions}
\pnum
\indextext{conversion!floating to integral}%
A prvalue of a floating-point type can be converted to a prvalue of an
integer type. The conversion truncates; that is, the fractional part is
discarded.
\indextext{value!undefined unrepresentable integral}%
The behavior is undefined if the truncated value cannot be represented
in the destination type.
\begin{note}
If the destination type is \tcode{bool}, see~\ref{conv.bool}.
\end{note}
\pnum
\indextext{conversion!integral to floating}%
\indextext{truncation}%
\indextext{rounding}%
A prvalue of an integer type or of an unscoped enumeration type can be converted to
a prvalue of a floating-point type. The result is exact if possible. If the value being
converted is in the range of values that can be represented but the value cannot be
represented exactly, it is an \impldef{value of result of inexact integer to
floating-point conversion} choice of either the next lower or higher representable
value. \begin{note} Loss of precision occurs if the integral value cannot be represented
exactly as a value of the floating-point type. \end{note} If the value being converted is
outside the range of values that can be represented, the behavior is undefined. If the
source type is \tcode{bool}, the value \tcode{false} is converted to zero and the value
\tcode{true} is converted to one.
\rSec1[conv.ptr]{Pointer conversions}
\pnum
\indextext{conversion!pointer}%
\indextext{null pointer conversion|see{conversion, null pointer}}%
\indextext{pointer!zero|see{value, null pointer}}%
\indextext{value!null pointer}%
A \defnx{null pointer constant}{constant!null pointer} is an \link{integer literal}{\ref{lex.icon}} with
value zero
or a prvalue of type \tcode{std::nullptr_t}. A null pointer constant can be
converted to a pointer type; the
result is the null pointer value of that type\iref{basic.compound} and is
distinguishable from every other value of
object pointer or function pointer
type.
Such a conversion is called a \defnx{null pointer conversion}{conversion!null pointer}.
Two null pointer values of the same type shall compare
equal. The conversion of a null pointer constant to a pointer to
cv-qualified type is a single conversion, and not the sequence of a
pointer conversion followed by a \link{qualification
conversion}{\ref{conv.qual}}. A null pointer constant of integral type
can be converted to a prvalue of type \tcode{std::nullptr_t}.
\begin{note} The resulting prvalue is not a null pointer value. \end{note}
\pnum
A prvalue of type ``pointer to \cv{} \tcode{T}'', where \tcode{T}
is an object type, can be converted to a prvalue of type ``pointer to
\cv{} \tcode{void}''.
The pointer value\iref{basic.compound} is unchanged by this conversion.
\pnum
A prvalue of type ``pointer to \cv{} \tcode{D}'', where \tcode{D}
is a class type, can be converted to a prvalue of type ``pointer to
\cv{} \tcode{B}'', where \tcode{B} is a base class\iref{class.derived}
of \tcode{D}. If \tcode{B} is an
inaccessible\iref{class.access} or
ambiguous\iref{class.member.lookup} base class of \tcode{D}, a program
that necessitates this conversion is ill-formed. The result of the
conversion is a pointer to the base class subobject of the derived class
object. The null pointer value is converted to the null pointer value of
the destination type.
\rSec1[conv.mem]{Pointer-to-member conversions}
\pnum
\indextext{conversion!pointer-to-member}%
\indextext{null member pointer conversion|see{conversion, null member pointer}}%
\indextext{constant!null pointer}%
A null pointer constant\iref{conv.ptr} can be converted to a
pointer-to-member
type; the result is the \defnx{null member pointer value}{value!null member pointer}
of that type and is distinguishable from any pointer to member not
created from a null pointer constant.
Such a conversion is called a \defnx{null member pointer conversion}{conversion!null member pointer}.
Two null member pointer values of
the same type shall compare equal. The conversion of a null pointer
constant to a pointer to member of cv-qualified type is a single
conversion, and not the sequence of a pointer-to-member conversion
followed by a \link{qualification conversion}{\ref{conv.qual}}.
\pnum
A prvalue of type ``pointer to member of \tcode{B} of type \cv{}
\tcode{T}'', where \tcode{B} is a class type, can be converted to
a prvalue of type ``pointer to member of \tcode{D} of type \cv{}
\tcode{T}'', where \tcode{D} is a derived class\iref{class.derived}
of \tcode{B}. If \tcode{B} is an
\link{inaccessible}{\ref{class.access}},
\link{ambiguous}{\ref{class.member.lookup}}, or \link{virtual}{\ref{class.mi}} base
class of \tcode{D}, or a base class of a virtual base class of
\tcode{D}, a program that necessitates this conversion is ill-formed.
The result of the conversion refers to the same member as the pointer to
member before the conversion took place, but it refers to the base class
member as if it were a member of the derived class. The result refers to
the member in \tcode{D}'s instance of \tcode{B}. Since the result has
type ``pointer to member of \tcode{D} of type \cv{} \tcode{T}'',
indirection through it with a \tcode{D} object is valid. The result is the same
as if indirecting through the pointer to member of \tcode{B} with the
\tcode{B} subobject of \tcode{D}. The null member pointer value is
converted to the null member pointer value of the destination
type.\footnote{The rule for conversion of pointers to members (from pointer to member
of base to pointer to member of derived) appears inverted compared to
the rule for pointers to objects (from pointer to derived to pointer to
base)~(\ref{conv.ptr}, \ref{class.derived}). This inversion is
necessary to ensure type safety. Note that a pointer to member is not
an object pointer or a function pointer
and the rules for conversions
of such pointers do not apply to pointers to members.
\indextext{conversion!pointer-to-member!\idxcode{void*}}%
In particular, a pointer to member cannot be converted to a
\tcode{void*}.}
\rSec1[conv.fctptr]{Function pointer conversions}
\pnum
\indextext{conversion!function pointer}%
A prvalue of type ``pointer to \tcode{noexcept} function''
can be converted to a prvalue of type ``pointer to function''.
The result is a pointer to the function.
A prvalue of type ``pointer to member of type \tcode{noexcept} function''
can be converted to a prvalue of type ``pointer to member of type function''.
The result designates the member function.
\begin{example}
\begin{codeblock}
void (*p)();
void (**pp)() noexcept = &p; // error: cannot convert to pointer to \tcode{noexcept} function
struct S { typedef void (*p)(); operator p(); };
void (*q)() noexcept = S(); // error: cannot convert to pointer to \tcode{noexcept} function
\end{codeblock}
\end{example}
\rSec1[conv.bool]{Boolean conversions}
\pnum
\indextext{conversion!boolean}%
A prvalue of arithmetic, unscoped enumeration, pointer, or pointer-to-member
type can be converted to a prvalue of type \tcode{bool}. A zero value, null
pointer value, or null member pointer value is converted to \tcode{false}; any
other value is converted to \tcode{true}. For
\deflink{direct-initialization}{\ref{dcl.init}}, a prvalue of type
\tcode{std::nullptr_t} can be converted to a prvalue of type
\tcode{bool}; the resulting value is \tcode{false}.
\indextext{conversion!standard|)}