title: How to create temporary object in C++
body: <p>I have the following piece of code.</p>
  
  <pre><code>class A {
   public:
    A(int) {
    }
  };
  
  int a;
  int main() {
    A(a);  // Line 'a
    return 0;
  }
  </code></pre>
  
  <p>What I want to do on line 'a is to create a temporary <code>A</code> with constructor <code>A::A(int)</code>. I know it's going to destruct immediately. That's what I want. But it seems the compiler is doing something equivalent to <code>A a</code>, defining variable <code>a</code> as of class <code>A</code> and initialize it using constructor <code>A::A()</code>. Of course it doesn't exist, hence the compiler error.</p>
  
  <p>However, if I change my code to the following.</p>
  
  <pre><code>class A {
   public:
    A(int) {
    }
  };
  
  void f(A) {
  }
  
  int a;
  int main() {
    f(A(a));
    return 0;
  }
  </code></pre>
  
  <p>It works fine now. The compiler constructs a temporary <code>A</code> and use it to call <code>f</code>.</p>
  
  <p>Why is <code>A(a)</code> different in both contexts? How is it stated in the standard or for some obscure reason? How can I construct a temporary object as in the first code sample?</p>