Well, I don't know if this is what you would call an anonymous function, or "Lambda" function I guess, I haven't really gotten that far into languages that use them yet...
but, I am writing a function that is rather long. And I want to just get through it, and then break it into smaller functions.
While I am writing it, I'm taking out pieces which I think I will later put into their own functions, by just wrapping them in { } braces. like, I know I can just do this:

int largefunc(){

	int a;
	
	{
		a=5
	}

	return a;

}

and I know this is totally incorrect, but I wonder if I can do something like it in C++:

int largefunc(){

	int a;
	
	a={
		return 5;
	}

	return a;

}

I suppose I don't really need this at all, it'd just be good to know if it's possible.

oh, also, totally unrelated, is it bad practice to put an assignment in an if statement? if((a=b)==5){ }

>>is it bad practice to put an assignment in an if statement?
IMO -- no, I have done that occasionally, especially in loops

int x;
while( (x = getnumber() ) == 0)
{
   // do something
}

Just remember scope when playing with those braces. If you declare something inside the braces, it will only exist between the braces:

int main(int argc, char **argv)
{

	int a;
	
	{
		int a;
		a=5;
	}

	cout << a << endl;
}

That is a whole different beast. Be careful of that "Gotcha!".

C++ does not have anonymous functions. However, C++0x standard does using the following syntax (using a std::for_each as an example)

std::map<int, int> someMap;
// ... ...
std::for_each(someMap.begin(), someMap.end(), []( std::pair<int, int> map_entry ) {
  // ... lambda body
});

The initial [] leading the arguments defined what should be passed to the lamda, [&] says pass everything by reference, [=] says pass everything by value, you can also pass variables individually which default to being passed by reference (Deference with * obviously)

But take note that this is only an initial standard and as far as I known there hasn't been a definite date nailed down.

okay, thanks--and I figured out the scope pretty quickly, it's weird how if you

int z;
{
	z=5;
}

then the z refers to the z outside the braces, but if you

int z;
{
	int z;
	z=5;
}

then it refers only to the z in the braces.

and @ Ancient Dragon, I see how that'd be useful I did some of those too, however I also crafted this...

if(switchType>0&&(
	   (switchType%2==0&&switches[index].switchType==SINGLE_SWITCH)
	 ||(switchType%2!=0&&switches[index].switchType==DOUBLE_SWITCH)
	 ||switches[index].switchType==COMPOUND_SWITCH)){

I can't decide if it's completely ugly or not.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.