Jump to content
  • Advertisement
Sign in to follow this  

Syntax weirdness

This topic is 4558 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement
I've never seen that syntax before (and indeed when I use it, it doesn't compile), but I have seen and used


int x(10);


Its like calling a constructor for the int and passing it the value you want it to be.

Share this post


Link to post
Share on other sites
Technically speaking, how can you give something some value if it doesn't exist, what the previous poster said is correct.

ace

Share this post


Link to post
Share on other sites
That statement on its own is not legal. In some contexts however it can be legal, for instance:
int x;

int main()
{
int x(x = 10);
}

Here the line int x(x = 10); defines a local variable x initialised to the result of assigning 10 to the global variable x.

Enigma

Share this post


Link to post
Share on other sites
well on g++ this is what I wrote... and it compiled without error giving output of 10

#include <iostream>

int main()
{
int x(x = 10);

std::cout << x << "\n";
return 0;
}




Can anyone explain this strangeness? To me this doesn't seem legal.

If this is a g++ bug it isn't the first time this has happened to me :>

Share this post


Link to post
Share on other sites
It's perfectly valid.

The C++ standard says:

The point of declaration for a name is immediately after its complete declarator (_dcl.decl_) and before its initializer (if any).

x is in scope when (x = 10) is evaluated, and any expression is permitted in a paranthesised initialiser.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!