title: "id" function in C++0x
body: <p>Reading <a href="https://stackoverflow.com/questions/6006527/overloading-on-r-value-references-and-code-duplication/6030496#6030496">this</a> answer regarding returning rvalue references from function got me thinking, how can I write an <code>id</code> function in C++0x. </p>
  
  <p>Basically, I want <code>id</code> to be a do nothing function, a function that has no observable effects on the program.</p>
  
  <p>My first attempt is the following:</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  class X
  {
  public:
    X(std::string&amp;&amp; s) : s(std::move(s)) {};
    X(const std::string&amp; s) : s(s) {};
    std::string s;
    ~X() { std::cout &lt;&lt; "Destroying: " &lt;&lt; s &lt;&lt; std::endl; }
  private:
    X(const X&amp;) {};
    X(X&amp;&amp;) {};
  };
  
  template &lt;class T&gt;
  T&amp;&amp; id(T&amp;&amp; x) { return static_cast&lt;T&amp;&amp;&gt;(x); }
  
  int main()
  {
    auto&amp;&amp; x1 = X("x1");
    std::cout &lt;&lt; "Line 1" &lt;&lt; std::endl;
    auto&amp;&amp; x2 = id(X("x2"));
    std::cout &lt;&lt; "Line 2" &lt;&lt; std::endl;
  }
  </code></pre>
  
  <p>However, I fear that in this case, x2 is a dangling reference, as <code>X("x2")</code> is destroyed before "Line 2" executes.</p>
  
  <p>So here, quite clearly <code>id</code> has an observable effect.</p>
  
  <p>How can I write an <code>id</code> function in C++0x, which in particular works for types without move/copy constructors.</p>