2012-12-14 101 views
3

我想使下面的代碼片段工作遞歸模板類型

typedef boost::function<result_type()> functor_type; 
typedef boost::variant<int, functor_type> result_type; 

兩種互相依賴,如何打破循環依賴?


的動機

基本上,我想要實現在C++中尾調用,這樣

template<typename ResultT> 
struct TailCall 
{ 
    typedef ResultT result_type; 
    typedef boost::function<ret_type()> continuation_type; 
    typedef boost::variant<result_type, continuation_type> ret_type; 

    TailCall(const continuation_type& init) : next(init) {} 

    result_type operator()() 
    { 
     while (true) { 
      ret_type r = next(); 
      result_type *result = boost::get<result_type>(&r); 
      if (result) 
       return *result; 
      else 
       next = boost::get<continuation_type>(r); 
     } 
    } 

private: 
    continuation_type next; 
}; 

TailCall<int>::ret_type fibonacci_impl(int term, int val, int prev) 
{ 
    if (term == 0) return prev; 
    if (term == 1) return val; 
    return boost::bind(fibonacci_impl, term-1, val+prev, val); 
} 

int fibonacci(int index) 
{ 
    TailCall<int> fib(boost::bind(fibonacci_impl, index, 1, 0)); 
    return fib(); 
} 
+0

爲什麼你需要嗎? –

+0

恐怕不可能做到這一點:( – Pubby

+1

如果你解釋(通過編輯問題)你正在嘗試做什麼,以及你將如何使用這些typedef類型,它可能會有所幫助。 – hyde

回答

1

升壓變體具有兩個特定的實用程序(recursive_variant和recursive_wrapper)做到這一點。

這裏使用一個例子recursive_wrapper

struct functor_type; 

typedef boost::variant<int, 
      boost::recursive_wrapper<functor_type> > result_type; 

struct functor_type : public boost::function<result_type()> { 
     using boost::function<result_type()>::boost::function<result_type()>; 
}; 
+0

謝謝,它的工作原理。 – user1903494

+0

其實我會很感激,如果有人可以發佈一個使用recursive_variant的解決方案,我無法設法得到這種情況下的工作。 – sbabbi

1

請看看boost::recursive_variant

一個例子從Boost.Spirit例子:

struct mini_xml; 

    typedef 
     boost::variant< 
      boost::recursive_wrapper<mini_xml> 
      , std::string 
     > 
    mini_xml_node; 

    struct mini_xml 
    { 
     std::string name;       // tag name 
     std::vector<mini_xml_node> children;  // children 
    }; 
}