C++ Newbie - Please judge my C++ code.

NegInfinity, have you looked at the assembly output of your suggestion:



FVector snap(FVector arg, float gridSize){
	auto f = &](float val){
		return gridSize ? (float)round(val/gridSize)*gridSize: val;		
	};
	return FVector{f(arg.X), f(arg.Y), f(arg.Z)};
}


I thought the same thing too with respect to having a single function being called to calculate the term. However, normally there is the cost of the push/pop/etc. associated with it. I don’t know how auto influences the assembly output. Have you looked at it?

In general, especially with applications that run in resource rich environments or with functions that aren’t being used too frequently, modularizing as much as possible is good programming practice. For me though, and for this specific problem, I wouldn’t incur the cost of the extra function calls. From a knee-jerk evaluation most of the statements can be executed in a linear fashion with the FPU. If the auto and use of it has no cost associated with the stack and execution management then I would use your approach.

If I had time I’d analyze and benchmark the two but I’m busy with other things at the moment.