*
pa**********@gmail.com:
Thanks for answers, and links.
When I posted my message I didn't think about accessing local
variables, so this is't really nested function. I good article about
that I found: http://www.gotw.ca/gotw/058.htm
I only have a question to Alf:
Yes, although for efficiency I wouldn't code it like that.
How then would you code this place? with #define's? For such simple
case compiler optimization will result in the equivalent code like the
following:
int f1(int x){
if(x > 0)
return x*x;
else
return -1;
}
As a rule, don't rely on compiler optimizations when you can express what
you want in code. For example, use const& arguments for strings etc. instead
of relying on compiler optimization. In such cases, and in this case,
optimization considerations can indicate more _natural_ ways to write the
code, ways that more directly reflect the intended effect:
int f( int x )
{
struct local
{
static int square( int x ) { return x*x; }
};
return (x > 0? local::square( x ) : -1);
}
Here there is no unused implicit argument to function 'square'.
However, it's probably Bad Advice to tell people to think about optimization,
because in most cases those who think optimization end up with code that's not
just unnatural, extremely complicated and inefficient, but also plain wrong...
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?