Creating a variable inside a function arguments

Pages: 12
I still don't see why you need to define a variable (int i = 3 is a definition, not a declaration, in this case) inside a function call.

Do you have an example of something it would allow you to something more easily than is currently possible?

Andy
I just wanted to ask, so it is all completely dependent on the people designing the standard on whether or not such a syntax would be confined to the function's scope.

So no current rules could be used to determine it.
Last edited on
Why is this thread so long?

It doesn't make sense. Why? Just why? What use would it serve?

@naraku9333: it is perfectly legal to place a function declaration inside any scope at any point, including function definition scopes and the scope of if statements, for loops, etc.
Last edited on
Yea I got it LB it's useless.
Just for anyone wondering

It is always confined to the function scope:

This code compiles its just that logically you dont need to give it a name:


1
2
3
4
5
6
7
8
9
int f(int a)
{
    return a;
}
int main()
{
    f(int(32));
    //return 0; // Can be omitted
}


Since its done in the function call, its confined to the function scope.

The reason it doesnt let you name it is because you will never be able to access it after the function call as Bourgond Aries said.
Last edited on
That is a cast, not a variable declaration.
Oh true, fail by me lol

I guess it doesnt serve any purpose at all.
Here's an example where it is allowed and does serve a purpose:
1
2
3
4
5
if(Library::Error *error = Library::DoSomething())
{
    std::cerr << "Error: " << error->toString() << std::endl;
    Library::FreeError(error);
}
This is similar to a library I am currently using. "error" is confined to the scope of the if statement, and the if statement evaluates to true if error is not 0 or nullptr.
It is allowed in conditions, loops and what not, just not functions.
Anmol444 wrote:
just not functions.
Correction: just not in function calls, or any expressions for that matter.
isnt a condition a expression?
1
2
3
4
int a = 0; //allowed
(int b = 0); //not allowed
if(int x = 1); //allowed
if((int y = 1)); //not allowed 
If you want to confuse the poor Anmol444 further,

1
2
switch(int n = 1) // allowed as well
    int m = 2; 
Ok, now I am really confused...

Why is the second and last one not allowed @LB

@Cubbi
I know thats allowed :P, but I forgot why.

Last edited on
Anmol444 wrote:
Why is the second and last one not allowed @LB
Because you can't declare variables in expressions? Lol, how would it even make sense?
Alright, just to prevent obfuscated code I guess. And it doesnt let you do anything new. Alright I am done with this.
Topic archived. No new replies allowed.
Pages: 12