fork(1) download
  1. #include <iostream>
  2. #include <memory>
  3. #include <vector>
  4. #include <functional>
  5.  
  6. class Node
  7. {
  8. public:
  9. Node ( std::string name ) : name__ (name){}
  10.  
  11. std::string name__;
  12.  
  13. bool operator== ( const Node & rhs ) const
  14. {
  15. return this->name__ == rhs.name__;
  16. }
  17. };
  18.  
  19. struct Edge
  20. {
  21. std::shared_ptr<Node> from;
  22. std::shared_ptr<Node> to;
  23. };
  24.  
  25. int main()
  26. {
  27. auto sky_node = std::make_shared<Node>( "sky" );
  28. auto is_node = std::make_shared<Node>( "is" );
  29. auto blue_node = std::make_shared<Node>( "blue" );
  30.  
  31. std::vector<Edge> edges = { {sky_node, is_node}, {is_node, blue_node} };
  32.  
  33. std::function<void ( const std::shared_ptr<Node>, unsigned int )> find_next;
  34. find_next = [=]( const std::shared_ptr<Node> node_to, unsigned int len )
  35. {
  36. for ( const auto rhs : edges )
  37. {
  38. if ( (*node_to) == (*rhs.from) )
  39. {
  40. len++;
  41. find_next ( rhs.from, len );
  42. }
  43. }
  44. };
  45.  
  46. for ( const auto lhs : edges )
  47. {
  48. unsigned int len = 0;
  49. const auto from = lhs.to;
  50. find_next ( from, len );
  51. }
  52.  
  53. return 0;
  54. }
Runtime error #stdin #stdout #stderr 0s 3236KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
terminate called after throwing an instance of 'std::bad_function_call'
  what():  bad_function_call