Preventing early object destruction
<p>In C++, if I write</p>

<pre><code>token make_token() { return token{}; }
</code></pre>

<p>and then use it as follows</p>

<pre><code>void use_token()
{
  make_token();
  // extra code
}
</code></pre>

<p>without assigning a token to a variable, <code>token</code>'s destructor fires before extra code executes. How can I get the destructor to only fire at the end of the function without having to make a variable?</p>

<p><em>Note:</em> I want to completely avoid making a variable. I know I can do <code>auto&amp; t = make_token()</code> or similar, but I want to avoid precisely this by returning <em>something</em> (I don't know what) that doesn't have the destructor fired immediately.</p>

<p><em>Why I want this:</em> basically, in my app (a compiler for a programming language) I have these things called tokens. A token's constructor can put a <code>{</code> and indent, and its destructor can then put <code>}</code> and un-indent. I thought it a good idea to set up functions which return these tokens by value, but I don't actually want to assign them to any value, since the tokens are useless and have no functions.</p>

<p><em>To alleviate confusion</em>, my <code>token</code> is <em>not</em> a lexical token. I use the work <code>token</code> in lieu of the work <code>cookie</code>. It's meant to do something in the constructor, wait until the end of its scope, and then do something in its destructor. That's it. By the way, if I was writing this in C#, I would write something like</p>

<pre><code> using (make_token())
 {
   // my code here
 }
</code></pre>

<p>and it would work as intended. But it turns out that something so simple is difficult in C++.</p>