Note that there are some explanatory texts on larger screens.

plurals
  1. POIs this the best way to do a "with" statement in C++?
    primarykey
    data
    text
    <p><strong>Edit:</strong></p> <p>So this question was misinterpreted to such a ludicrous degree that it has no point anymore. I don't know <em>how</em>, since the question that I actually <em>asked</em> was whether <em>my specific implementation</em> of this&mdash;yes, known to be pointless, yes, not remotely resembling idiomatic C++&mdash;macro was as good as it could be, and whether it necessarily had to use <code>auto</code>, or if there was a suitable workaround instead. It was not supposed to generate this much attention, and certainly not a misunderstanding of this magnitude. It's pointless to ask respondents to edit their answers, I don't want anybody to lose reputation over this, and there's some good information floating around in here for potential future viewers, so I'm going to arbitrarily pick one of the lower-voted answers to evenly distribute the reputation involved. Move along, nothing to see here.</p> <hr> <p>I saw <a href="https://stackoverflow.com/questions/601153/missing-the-with-keyword-in-c">this question</a> and decided it might be fun to write a <code>with</code> statement in C++. The <code>auto</code> keyword makes this really easy, but is there a better way to do it, perhaps without using <code>auto</code>? I've elided certain bits of the code for brevity.</p> <pre><code>template&lt;class T&gt; struct with_helper { with_helper(T&amp; v) : value(v), alive(true) {} T* operator-&gt;() { return &amp;value; } T&amp; operator*() { return value; } T&amp; value; bool alive; }; template&lt;class T&gt; struct with_helper&lt;const T&gt; { ... }; template&lt;class T&gt; with_helper&lt;T&gt; make_with_helper(T&amp; value) { ... } template&lt;class T&gt; with_helper&lt;const T&gt; make_with_helper(const T&amp; value) { ... } #define with(value) \ for (auto o = make_with_helper(value); o.alive; o.alive = false) </code></pre> <p>Here's an (updated) usage example with a more typical case that shows the use of <code>with</code> as it is found in other languages.</p> <pre><code>int main(int argc, char** argv) { Object object; with (object) { o-&gt;member = 0; o-&gt;method(1); o-&gt;method(2); o-&gt;method(3); } with (object.get_property("foo").perform_task(1, 2, 3).result()) { std::cout &lt;&lt; (*o)[0] &lt;&lt; '\n' &lt;&lt; (*o)[1] &lt;&lt; '\n' &lt;&lt; (*o)[2] &lt;&lt; '\n'; } return 0; } </code></pre> <p>I chose <code>o</code> because it's an uncommon identifier, and its form gives the impression of a "generic thing". If you've got an idea for a better identifier or a more usable syntax altogether, then please do suggest it.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload