struct X
{
X() { std::cout << "X()\n"; }
X(int) { std::cout << "X(int)\n"; }
};
const int answer = 42;
int main()
{
X(answer);
}
I would have expected this to print either
X(int)
, becauseX(answer);
could be interpreted as a cast fromint
toX
, or- nothing at all, because
X(answer);
could be interpreted as the declaration of a variable.
However, it prints X()
, and I have no idea why X(answer);
would call the default constructor.
BONUS POINTS: What would I have to change to get a temporary instead of a variable declaration?
int answer
-> any other type. – DiversifyX(int(answer));
doesn't print anything, because it's a function declaration :) – Knottdouble(expresso);
there you go, declared just for you ;) – Knott