#include <iostream>
#include <functional>
struct wrap
{
std:: function < wrap ( ) > f;
template < typename F>
wrap( F f)
: f( f)
{
}
wrap operator( ) ( )
{
return f( ) ;
}
} ;
wrap menu_sub_1( ) ;
wrap menu_sub_2( ) ;
wrap menu_main( )
{
std:: cout
<< "Main menu" << std:: endl
<< "1. Sub menu 1" << std:: endl
<< "2. Sub menu 2" << std:: endl
<< "3. Exit" << std:: endl ;
int x;
std:: cin >> x;
if ( x == 1 ) return menu_sub_1;
else if ( x == 2 ) return menu_sub_2;
else if ( x == 3 ) std:: exit ( 0 ) ;
return menu_main;
}
wrap menu_sub_1( )
{
std:: cout
<< "Sub menu 1" << std:: endl
<< "1. Back to main menu" << std:: endl ;
int x;
std:: cin >> x;
if ( x == 1 ) return menu_main;
return menu_sub_1;
}
wrap menu_sub_2( )
{
std:: cout
<< "Sub menu 2" << std:: endl
<< "1. Back to main menu" << std:: endl ;
int x;
std:: cin >> x;
if ( x == 1 ) return menu_main;
return menu_sub_2;
}
int main( )
{
auto f = menu_main( ) ;
while ( true )
{
f = f( ) ;
}
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8ZnVuY3Rpb25hbD4KCnN0cnVjdCB3cmFwCnsKCXN0ZDo6ZnVuY3Rpb248d3JhcCAoKT4gZjsKCXRlbXBsYXRlPHR5cGVuYW1lIEY+Cgl3cmFwKEYgZikKCTogZihmKQoJewoJfQoJd3JhcCBvcGVyYXRvcigpKCkKCXsKCQlyZXR1cm4gZigpOwoJfQp9OwoKd3JhcCBtZW51X3N1Yl8xKCk7CndyYXAgbWVudV9zdWJfMigpOwp3cmFwIG1lbnVfbWFpbigpCnsKCXN0ZDo6Y291dAoJCTw8ICJNYWluIG1lbnUiIDw8IHN0ZDo6ZW5kbAoJCTw8ICIxLiBTdWIgbWVudSAxIiA8PCBzdGQ6OmVuZGwKCQk8PCAiMi4gU3ViIG1lbnUgMiIgPDwgc3RkOjplbmRsCgkJPDwgIjMuIEV4aXQiIDw8IHN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