Я хочу сделать следующий фрагмент кода для работырекурсивные типы шаблонов
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();
}
Зачем вам это нужно? –
Я боюсь, что это невозможно сделать :( – Pubby
Это может помочь, если вы объясните (путем редактирования вопроса), что вы пытаетесь сделать, и как вы собираетесь использовать эти типы typedef. – hyde