Why does this call the default constructor?

nothing at all, because X(answer); could be interpreted as the declaration of a variable.

Your answer is hidden in here. If you declare a variable, you invoke its default ctor (if non-POD and all that stuff).

On your edit: To get a temporary, you have a few options:

  • (X(answer));
  • (X)answer;
  • static_cast<X>(answer)
  • X{answer}; (C++11)
  • []{ return X(answer); }(); (C++11, may incur copy)
  • void(), X(answer);
  • X((void(),answer));
  • true ? X(answer) : X();
  • if(X(answer), false){}
  • for(;X(answer), false;);
  • X(+answer);

Leave a Comment