-
Notifications
You must be signed in to change notification settings - Fork 0
/
printing_volatile_pointers.bs
244 lines (206 loc) · 6.69 KB
/
printing_volatile_pointers.bs
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
<!--
Copyright (c) 2018 NVIDIA Corporation
Author: Bryce Adelstein Lelbach <brycelelbach@gmail.com>
Distributed under the Boost Software License v1.0 (boost.org/LICENSE_1_0.txt)
-->
<pre class='metadata'>
Title: Printing `volatile` Pointers
H1: Printing <code>volatile</code> Pointers
Shortname: D1147
Revision: 2
Status: D
Group: WG21
Audience: LEWG
URL: https://wg21.link/P1147
!Source: <a href="https://github.com/brycelelbach/wg21_p1147_printing_volatile_pointers/blob/master/printing_volatile_pointers.bs">github.com/wg21_p1147_printing_volatile_pointers/blob/master/printing_volatile_pointers.bs</a>
Issue Tracking: GitHub https://github.com/brycelelbach/wg21_p1147_printing_volatile_pointers/issues
Editor: Bryce Adelstein Lelbach, NVIDIA https://nvidia.com, brycelelbach@gmail.com
Metadata Order: Editor, Source, Issue Tracking, Project, Audience
Markup Shorthands: markdown yes
No Abstract: yes
Toggle Diffs: no
Boilerplate: style-syntax-highlighting off
</pre>
<style>
pre {
margin-top: 0px;
margin-bottom: 0px;
}
.ins, ins, ins *, span.ins, span.ins * {
background-color: rgb(200, 250, 200);
color: rgb(0, 136, 0);
text-decoration: none;
}
.del, del, del *, span.del, span.del * {
background-color: rgb(250, 200, 200);
color: rgb(255, 0, 0);
text-decoration: line-through;
text-decoration-color: rgb(255, 0, 0);
}
math, span.math {
font-family: serif;
font-style: italic;
}
ul {
list-style-type: "— ";
}
blockquote {
counter-reset: paragraph;
}
div.numbered, div.newnumbered {
margin-left: 2em;
margin-top: 1em;
margin-bottom: 1em;
}
div.numbered:before, div.newnumbered:before {
position: absolute;
margin-left: -2em;
display-style: block;
}
div.numbered:before {
content: counter(paragraph);
counter-increment: paragraph;
}
div.newnumbered:before {
content: "�";
}
div.numbered ul, div.newnumbered {
counter-reset: list_item;
}
div.numbered li, div.newnumbered {
margin-left: 3em;
}
div.numbered li:before, div.newnumbered li:before {
position: absolute;
margin-left: -4.8em;
display-style: block;
}
div.numbered li:before {
content: "(" counter(paragraph) "." counter(list_item) ")";
counter-increment: list_item;
}
div.newnumbered li:before {
content: "(�." counter(list_item) ")";
counter-increment: list_item;
}
</style>
# Introduction # {#intro}
Printing pointers to `volatile` types with standard library output streams has
unexpected results.
Consider the following code:
```
#include <iostream>
int main()
{
int* p0 = reinterpret_cast< int*>(0xdeadbeef);
volatile int* p1 = reinterpret_cast<volatile int*>(0xdeadbeef);
std::cout << p0 << std::endl;
std::cout << p1 << std::endl;
}
```
This produces the following output:
```
0xdeadbeef
1
```
What happened here? Well, `basic_ostream` has an `operator<<` for both `const void*`
and `bool`:
<blockquote>
<b>27.7.5.1 Class template `basic_ostream` <span style="float: right;">[ostream]</span></b>
<xmp>
namespace std {
template<class charT, class traits = char_traits<charT>>
class basic_ostream : virtual public basic_ios<charT, traits> {
public:
// ...
// [ostream.formatted], formatted output
// ...
basic_ostream<charT, traits>& operator<<(bool n);
basic_ostream<charT, traits>& operator<<(const void* p);
// ...
// ...
};
}
</xmp>
</blockquote>
For `std::cout << p0`, `p0` (an `int*`) is implicitly converted to `const
void*` and `operator<<(const void*)` is called.
However, for `std::cout << p1`, the `operator<<(const void*)` overload is not a
match, as it discards qualifiers. Instead, the best match is
`operator<<(bool)`, so `p1` (a `volatile int*`) is implicitly converted to
`bool` and `operator<<(bool)` is called.
I suggest we add a new `operator<<(const volatile void*)` overload that
`const_cast`s away `volatile` and calls the `operator<<(const void*)`
overload.
Initially, I explored modifying the underlying `<locale>` methods and changing
the existing `operator<<(const void*)` to `operator<<(const volatile void*)`,
however, early feedback from other committee members pushed me away from this
direction, as there was concern that modifying `<locale>` would be an ABI
breaking change.
Note that `const_cast`ing away `volatile` is safe here.
We are not accessing or printing the value of the object that the pointer
points to, we are just printing the value of the pointer itself.
# Wording # {#wording}
Modify <a href="http://eel.is/c++draft/ostream.general">[**ostream.general**]</a> as follows:
<blockquote>
<b>29.7.5.2.1 General <span style="float: right;">[ostream.general]</span></b>
<pre><code>
namespace std {
template<class charT, class traits = char_traits<charT>>
class basic_ostream : virtual public basic_ios<charT, traits> {
public:
// ...
// [ostream.formatted], formatted output
basic_ostream& operator<<(basic_ostream& (*pf)(basic_ostream&));
basic_ostream& operator<<(basic_ios<charT, traits>& (*pf)(basic_ios<charT, traits>&));
basic_ostream& operator<<(ios_base& (*pf)(ios_base&));
basic_ostream& operator<<(bool n);
basic_ostream& operator<<(short n);
basic_ostream& operator<<(unsigned short n);
basic_ostream& operator<<(int n);
basic_ostream& operator<<(unsigned int n);
basic_ostream& operator<<(long n);
basic_ostream& operator<<(unsigned long n);
basic_ostream& operator<<(long long n);
basic_ostream& operator<<(unsigned long long n);
basic_ostream& operator<<(float f);
basic_ostream& operator<<(double f);
basic_ostream& operator<<(long double f);
basic_ostream& operator<<(const void* p);
<span class="ins"> basic_ostream& operator<<(const volatile void* val);</span>
basic_ostream& operator<<(nullptr_t);
basic_ostream& operator<<(basic_streambuf<char_type, traits>* sb);
// ...
};
// ...
}
</code></pre>
</blockquote>
Modify <a href="http://eel.is/c++draft/ostream.inserters">[**ostream.inserters**]</a> as follows:
<blockquote>
<b>29.7.5.3.3 <code>basic_ostream::operator<<</code> <span style="float: right;">[ostream.inserters]</span></b>
...
<pre><code>
basic_ostream& operator<<(nullptr_t);
</code></pre>
<div class="numbered" style="counter-reset: paragraph 10">
*Effects:* Equivalent to:
<pre><code>
return *this << s;
</code></pre>
where `s` is an implementation-defined NTCTS.
</div>
<div class="ins">
<pre><code>
basic_ostream& operator<<(const volatile void* val);
</code></pre>
<div class="numbered">
*Effects:* Equivalent to:
<pre><code>
return operator<<(const_cast<const void*>(val));
</code></pre>
</div>
</blockquote>
# Acknowledgements # {#acknowledgements}
Thanks to JF Bastien, Marshall Clow, Billy O'Neal, Louis Dionne, Jonathan Wakely,
and Jeff Garland for reviewing and providing feedback on this paper.