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){ }

Dani AI

Generated

Short answer: modern C++ gives you anonymous functions (lambdas) so you can write a little expression-like block that returns a value and keep temporaries scoped tightly. That is a direct, safe substitute for the "I just want a block that returns a value" pattern you sketched with braces.

Example of an immediately-invoked local lambda (keeps temporaries inside the block):

int a = [&]() {
int tmp = / complex logic, local temps, etc. /;
return tmp;
}();

A few practical notes beyond the thread:

  • Scope: as warned, inner braces create a new scope; lambdas let you do the same but also return values. Never return a lambda that captures locals by reference — that creates a dangling reference.
  • Capture semantics: capture-by-value vs capture-by-reference affect lifetime and behavior; mutable lets a lambda modify its captured-by-value copies. Be careful when lambdas are used asynchronously (threads, deferred tasks).
  • Modern features: later standards added useful lambda tools (init-capture, generic lambdas, template parameter lists) that make local helpers compact and expressive. Use std::function when you need type erasure, but be aware of its overhead.
  • Assignment in if: assigning in a condition is legal and occasionally useful (as showed), but is error-prone. Prefer the clearer C++17 form that lets you initialise and test in one statement, or just split the assignment and test into two lines. Turn on compiler warnings (GCC/Clang -Wall -Wextra) or use clang-tidy to catch accidental = in conditions.

References for full rules and examples:

Credit: the question and examples from , and the scope/assignment/lambda comments from , , and helped shape these practical suggestions.

Recommended Answers

All 4 Replies

>>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.