main
tink 1 year ago
parent 9c99038da9
commit c609d23b01

@ -112,7 +112,7 @@ Marshall Cline's advice can be improved, though. Here's a recipe that will effor
Let's apply this recipe to the following (by itself irrelevant) complex declaration. Little arrows indicate how far we should read at each step and the direction of the arrow indicates the reading direction:
```
```bash
char const *(* const (*(*ip)())[])[]
ip Start at the variable's name:

@ -148,11 +148,11 @@ Conditions are represented by the following condition flags:
Several condition member functions are available to manipulate or determine the states of `ios` objects. Originally they returned `int` values, but their current return type is `bool`:
- bool ios::bad():
- **bool ios::bad()**:
the value true is returned when the stream's badbit has been set and false otherwise. If true is returned it indicates that an illegal operation has been requested at the level of the streambuf object to which the stream interfaces. What does this mean? It indicates that the streambuf itself is behaving unexpectedly. Consider the following example:
the value true is returned when the stream's `badbit` has been set and false otherwise. If true is returned it indicates that an illegal operation has been requested at the level of the streambuf object to which the stream interfaces. What does this mean? It indicates that the streambuf itself is behaving unexpectedly. Consider the following example:
```cpp
std::ostream error(0);
std::ostream error(0);
```
Here an `ostream` object is constructed without providing it with a working `streambuf` object. Since this `streambuf` will never operate properly, its `badbit` flag is raised from the very beginning: `error.bad()` returns true.
@ -1648,7 +1648,7 @@ In this example
Like `fstream` objects string-stream objects can also be used for reading and writing. After including the `<sstream>` header file a `std::stringstream` can be defined which supports both reading and writing. After inserting information into a `stringstream` object `seekg(0)` can be called to read its info from the beginning of its content. When a `stringstream` must repeatedly be used for reading and writing call its `clear` and `str` members before starting a new writing cycle. Alternatively, a `stringstream` str can be reinitialized using `str = stringstream{}`. Here is an example:
```
```cpp
#include <iostream>
#include <sstream>
using namespace std;

@ -256,36 +256,36 @@ https://github.com/alex-shpak/hugo-book
<li>If you reached an opening parenthesis, continue at step 2 beyond the parenthesis where you previously stopped.</li>
</ol>
<p>Let&rsquo;s apply this recipe to the following (by itself irrelevant) complex declaration. Little arrows indicate how far we should read at each step and the direction of the arrow indicates the reading direction:</p>
<pre tabindex="0"><code>char const *(* const (*(*ip)())[])[]
ip Start at the variable&#39;s name:
&#39;ip&#39; is
ip) Hitting a closing paren: revert
--&gt;
(*ip) Find the matching open paren:
&lt;- &#39;a pointer to&#39;
(*ip)()) The next unmatched closing par:
--&gt; &#39;a function (not expecting
arguments)&#39;
(*(*ip)()) Find the matching open paren:
&lt;- &#39;returning a pointer to&#39;
(*(*ip)())[]) The next closing par:
--&gt; &#39;an array of&#39;
(* const (*(*ip)())[]) Find the matching open paren:
&lt;-------- &#39;const pointers to&#39;
(* const (*(*ip)())[])[] Read until the end:
-&gt; &#39;an array of&#39;
char const *(* const (*(*ip)())[])[] Read backwards what&#39;s left:
&lt;----------- &#39;pointers to const chars&#39;
</code></pre><p>Collecting all the parts, we get for char <code>const *(* const (*(*ip)())[])[]</code>: <code>ip</code> is a pointer to a function (not expecting arguments), returning a pointer to an array of const pointers to an array of pointers to const chars. This is what <code>ip</code> represents; the recipe can be used to parse any declaration you ever encounter.</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>char const *<span style="color:#f92672">(</span>* const <span style="color:#f92672">(</span>*<span style="color:#f92672">(</span>*ip<span style="color:#f92672">)())[])[]</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span> ip Start at the variable<span style="color:#e6db74">&#39;s name:
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74"> &#39;</span>ip<span style="color:#e6db74">&#39; is
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74"> ip) Hitting a closing paren: revert
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74"> --&gt;
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74"> (*ip) Find the matching open paren:
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74"> &lt;- &#39;</span>a pointer to<span style="color:#e6db74">&#39;
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74"> (*ip)()) The next unmatched closing par:
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74"> --&gt; &#39;</span>a <span style="color:#66d9ef">function</span> <span style="color:#f92672">(</span>not expecting
</span></span><span style="display:flex;"><span> arguments<span style="color:#f92672">)</span><span style="color:#e6db74">&#39;
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74"> (*(*ip)()) Find the matching open paren:
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74"> &lt;- &#39;</span>returning a pointer to<span style="color:#e6db74">&#39;
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74"> (*(*ip)())[]) The next closing par:
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74"> --&gt; &#39;</span>an array of<span style="color:#e6db74">&#39;
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74"> (* const (*(*ip)())[]) Find the matching open paren:
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74"> &lt;-------- &#39;</span>const pointers to<span style="color:#e6db74">&#39;
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74"> (* const (*(*ip)())[])[] Read until the end:
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74"> -&gt; &#39;</span>an array of<span style="color:#e6db74">&#39;
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">char const *(* const (*(*ip)())[])[] Read backwards what&#39;</span>s left:
</span></span><span style="display:flex;"><span>&lt;----------- <span style="color:#e6db74">&#39;pointers to const chars&#39;</span>
</span></span></code></pre></div><p>Collecting all the parts, we get for char <code>const *(* const (*(*ip)())[])[]</code>: <code>ip</code> is a pointer to a function (not expecting arguments), returning a pointer to an array of const pointers to an array of pointers to const chars. This is what <code>ip</code> represents; the recipe can be used to parse any declaration you ever encounter.</p>
<h3 id="312-namespaces">
3.1.2: Namespaces
<a class="anchor" href="#312-namespaces">#</a>

@ -352,9 +352,9 @@ https://github.com/alex-shpak/hugo-book
<p>Several condition member functions are available to manipulate or determine the states of <code>ios</code> objects. Originally they returned <code>int</code> values, but their current return type is <code>bool</code>:</p>
<ul>
<li>
<p>bool ios::bad():</p>
<p>the value true is returned when the stream&rsquo;s badbit has been set and false otherwise. If true is returned it indicates that an illegal operation has been requested at the level of the streambuf object to which the stream interfaces. What does this mean? It indicates that the streambuf itself is behaving unexpectedly. Consider the following example:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-cpp" data-lang="cpp"><span style="display:flex;"><span> std<span style="color:#f92672">::</span>ostream error(<span style="color:#ae81ff">0</span>);
<p><strong>bool ios::bad()</strong>:</p>
<p>the value true is returned when the stream&rsquo;s <code>badbit</code> has been set and false otherwise. If true is returned it indicates that an illegal operation has been requested at the level of the streambuf object to which the stream interfaces. What does this mean? It indicates that the streambuf itself is behaving unexpectedly. Consider the following example:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-cpp" data-lang="cpp"><span style="display:flex;"><span>std<span style="color:#f92672">::</span>ostream error(<span style="color:#ae81ff">0</span>);
</span></span></code></pre></div><p>Here an <code>ostream</code> object is constructed without providing it with a working <code>streambuf</code> object. Since this <code>streambuf</code> will never operate properly, its <code>badbit</code> flag is raised from the very beginning: <code>error.bad()</code> returns true.</p>
</li>
<li>
@ -1757,26 +1757,26 @@ the current object is reinitialized with new initial content.</p>
</li>
</ul>
<p>Like <code>fstream</code> objects string-stream objects can also be used for reading and writing. After including the <code>&lt;sstream&gt;</code> header file a <code>std::stringstream</code> can be defined which supports both reading and writing. After inserting information into a <code>stringstream</code> object <code>seekg(0)</code> can be called to read its info from the beginning of its content. When a <code>stringstream</code> must repeatedly be used for reading and writing call its <code>clear</code> and <code>str</code> members before starting a new writing cycle. Alternatively, a <code>stringstream</code> str can be reinitialized using <code>str = stringstream{}</code>. Here is an example:</p>
<pre tabindex="0"><code>#include &lt;iostream&gt;
#include &lt;sstream&gt;
using namespace std;
int main(int argc, char **argv)
{
stringstream io;
for (size_t redo = 0; redo != 2; ++redo)
{
io.clear(); // clears the not-good flags
io.str(&#34;&#34;);
io &lt;&lt; argv[0] &lt;&lt; &#39;\n&#39;;
io.seekg(0);
string line;
while (getline(io, line)) // results in io.eof()
cout &lt;&lt; line &lt;&lt; &#39;\n&#39;;
}
}
</code></pre></article>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-cpp" data-lang="cpp"><span style="display:flex;"><span><span style="color:#75715e">#include</span> <span style="color:#75715e">&lt;iostream&gt;</span><span style="color:#75715e">
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">#include</span> <span style="color:#75715e">&lt;sstream&gt;</span><span style="color:#75715e">
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">using</span> <span style="color:#66d9ef">namespace</span> std;
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">int</span> <span style="color:#a6e22e">main</span>(<span style="color:#66d9ef">int</span> argc, <span style="color:#66d9ef">char</span> <span style="color:#f92672">**</span>argv)
</span></span><span style="display:flex;"><span>{
</span></span><span style="display:flex;"><span> stringstream io;
</span></span><span style="display:flex;"><span> <span style="color:#66d9ef">for</span> (size_t redo <span style="color:#f92672">=</span> <span style="color:#ae81ff">0</span>; redo <span style="color:#f92672">!=</span> <span style="color:#ae81ff">2</span>; <span style="color:#f92672">++</span>redo)
</span></span><span style="display:flex;"><span> {
</span></span><span style="display:flex;"><span> io.clear(); <span style="color:#75715e">// clears the not-good flags
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span> io.str(<span style="color:#e6db74">&#34;&#34;</span>);
</span></span><span style="display:flex;"><span> io <span style="color:#f92672">&lt;&lt;</span> argv[<span style="color:#ae81ff">0</span>] <span style="color:#f92672">&lt;&lt;</span> <span style="color:#e6db74">&#39;\n&#39;</span>;
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span> io.seekg(<span style="color:#ae81ff">0</span>);
</span></span><span style="display:flex;"><span> string line;
</span></span><span style="display:flex;"><span> <span style="color:#66d9ef">while</span> (getline(io, line)) <span style="color:#75715e">// results in io.eof()
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span> cout <span style="color:#f92672">&lt;&lt;</span> line <span style="color:#f92672">&lt;&lt;</span> <span style="color:#e6db74">&#39;\n&#39;</span>;
</span></span><span style="display:flex;"><span> }
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div></article>

Loading…
Cancel
Save