C++ 11 and Boost Quizzes

C++ 11 and Boost Quizzes

Postby Cuchulainn » Wed Apr 24, 2013 3:36 pm

Q: how can we define multi-cast std::function natively in C++ 11? With boost::signals it is possible but I need 2 separate overloaded functions.



(C# calls them multicast delegates)









const long N = 10;

std::vector<Point> bigArr(N);

for (std::size_t j = 0; j < bigArr.size()-1; ++j)

{

bigArr[j].x = double(j);

bigArr[j].y = double(j+1);



}



WeightedDiGraph<Point, double> bigGraph(bigArr);



for (std::size_t j = 0; j < bigArr.size()-1; ++j)

{

bigGraph.path(j,j+1, double(j/2.0));



}









// Using std::function, need to call each command separately

bigGraph.bftraverse(3, translate);

bigGraph.debug_print();

bigGraph.bftraverse(3, scale);

//bigGraph.debug_print();



// Multicast traversal

boost::signal<void (Point& pt)> mySignal;

mySignal.connect(&translate);

mySignal.connect(&scale);



bigGraph.bftraverse(0, mySignal);

bigGraph.debug_print();





//





void translate(Point& p) // T == Point

{

p.x += 2.0;

p.y += 7.0;

}



void scale(Point& p) // T == Point

{

p.x *= 0.5;

p.y *= 2.0;

}
User avatar
Cuchulainn
 
Posts: 669
Joined: Mon Dec 18, 2006 2:48 pm
Location: Amsterdam, the Netherlands

Return to C++

Who is online

Users browsing this forum: No registered users and 1 guest

cron