I have a piece of code like this:
std::list<boost::shared_ptr<Point> > left, right;
double alpha = angle(*(right.begin()->get()), *(((++right.begin()))->get()), *(left.begin()->get()) );
std::cout << alpha * 180 / M_PI << std::endl;
if(alpha < 0){
}
alpha = angle(*(right.begin()->get()), *(((++right.begin()))->get()), *(left.begin()->get()) );
Besides the iterator increment magic, which cannot be too obvoius here without comment, I would like to define a function double alpha()to reduce duplication. But since the use of this function is very specific, I would like to make it local. Perfectly:
int a, b;
int sum(){ return a + b; }
a = 5; b = 6;
int s = sum();
a = 3;
s = sum();
In languages like Python, this will be fine, but how to do it in C ++?
EDIT:
Here is what I got, special thanks to @wilx and flags -std=c++0x:
auto alpha = [&right, &left]() {
// not 100% correct due to my usage of boost::shared_ptr, but to get the idea
Point r_first = *(right.begin());
Point l_first = *(left.begin());
Point r_second = *(++right.begin());
return angle(r_first, r_second, l_first);
};
if(alpha() < 0) // fix it
double new_alpha = alpha();
source
share