80
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), because X(answer); could be interpreted as a cast from int to X, 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?

curiousguy
  • 8,038
  • 2
  • 40
  • 58
fredoverflow
  • 256,549
  • 94
  • 388
  • 662

3 Answers3

73

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:

Xeo
  • 129,499
  • 52
  • 291
  • 397
  • 4
    The `static_cast(answer)` feels the "most C++" answer -- it's even recommended by an old [GCC documentation](http://gcc.gnu.org/onlinedocs/gcc-4.0.2/gcc/Volatiles.html) as a way to force an rvalue. – Kerrek SB Jul 27 '12 at 16:23
  • Won't the brace initializer also maybe incur a copy? – rubenvb Jul 27 '12 at 16:45
  • @rubenvb: Why would it? It's just a fancy new way of saying `X(answer)` and guarantees a ctor call. – Xeo Jul 27 '12 at 16:50
  • @Xeo: because brace initializer syntax takes its arguments by value? (<- note the question mark) – rubenvb Jul 27 '12 at 16:51
  • @rubenvb: Oh, I didn't mean that the `answer` is copied, I meant when the `X` is returned from inside the lambda. – Xeo Jul 27 '12 at 16:52
  • 4
    @KerrekSB But surely only before C++11, no? Now, the canonical answer will be `X{answer}`. – Konrad Rudolph Jul 28 '12 at 11:48
  • Can anybody please explain how `X(answer)` is equivalent to `X answer` ? – Sam Aug 18 '13 at 19:48
  • @SAM: Because the standard says so. As Kerrek's answer says, the parens are optional. – Xeo Aug 18 '13 at 20:09
  • @Xeo Common man! Just tell me how do you know every little detail about c++? I'm not asking about just this question but I've seen your other answers too. I mean, just look at`void(), X(answer);` How on earth did you find that out?!?! Also what do you refer for C++? I read TC++PL (3rd & 4th edition). Also how did you learned C++11 in such a short time? – Sam Aug 19 '13 at 06:17
  • @Xeo And how many years of programming experience did you require to get to this stage? – Sam Aug 19 '13 at 06:25
  • @SAM: I recommend joining me in the Lounge chat room (check my profile), since this is getting off-topic here. :) – Xeo Aug 19 '13 at 08:19
67

The parentheses are optional. What you said is identical to X answer;, and it's a declaration statement.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
9

If you want to declare a variable of the type X, you should do it this way:

X y(answer);
huysentruitw
  • 27,376
  • 9
  • 90
  • 133
  • 1
    He did not ask how to make it call the `X(int)` ctor. – Xeo Jul 27 '12 at 15:41
  • Yeah but I have a small feeling that it was the thing he ment to do :) – huysentruitw Jul 27 '12 at 15:42
  • 6
    @WouterH: Actually, knowing Fred, it's unlikely. He's one of those folks who like to explore the dark corners of the C++ Standard and try and understand it. In a certain RPG he would have lost all his sanity points already ;) – Matthieu M. Jul 27 '12 at 17:40