[英]Recursive typedef function definition : std::function returning its own type
我正在嘗試實現一個狀態機。 狀態由callback_t
類型的函數表示: callback_t(int&)
,它返回相同類型的函數。
我不知道如何實現它,因為似乎不允許遞歸類型函數。
在這里我嘗試(作為玩具):
#include <stdio.h>
#include <functional>
typedef std::function< callback_t(int &) > callback_t ;
callback_t f1(int & i)
{
i++;
return f1;
}
callback_t f0(int & i)
{
if(i==0) i++;
return f1;
}
callback_t start(int & i)
{
i=0;
return f0;
}
int main(int argc, char **argv)
{
callback_t begin = start;
int i=0;
while(i<100)
begin = begin(i);
printf("hello world\n");
return 0;
}
錯誤:
C:/work/tests/tests/main.cpp:4:41: error: 'callback_t' was not declared in this scope
typedef std::function< callback_t(int &) > callback_t ;
^
有沒有辦法實現這種行為?
環境:win7,codelite,mingw 4.8.1
由於無法進行遞歸類型定義,因此可以聲明一個包含該函數並隱式轉換為該函數的結構:
template< typename... T >
struct RecursiveHelper
{
typedef std::function< RecursiveHelper(T...) > type;
RecursiveHelper( type f ) : func(f) {}
operator type () { return func; }
type func;
};
typedef RecursiveHelper<int&>::type callback_t;
一種用於將一種類型替換為另一種類型的小型庫:
template<class T>struct tag{using type=T;};
template<class X, class A, class B> struct subst:tag<X>{};
template<class X, class A, class B>
using subst_t=typename subst<X,A,B>::type;
template<class A, class B> struct subst<A,A,B>:tag<B>{};
template<class X, class A, class B>
struct subst<X&,A,B>:tag<subst_t<X,A,B>&>{};
template<class X, class A, class B>
struct subst<X&&,A,B>:tag<subst_t<X,A,B>&&>{};
template<class X, class A, class B>
struct subst<X const,A,B>:tag<subst_t<X,A,B>const>{};
template<class X, class A, class B>
struct subst<X volatile,A,B>:tag<subst_t<X,A,B>volatile>{};
template<class X, class A, class B>
struct subst<X const volatile,A,B>:tag<subst_t<X,A,B>const volatile>{};
template<template<class...>class Z,class...Xs, class A, class B>
struct subst<Z<Xs...>,A,B>:tag<Z<subst_t<Xs,A,B>...>>{};
template<template<class,size_t>class Z,class X,size_t n, class A, class B>
struct subst<Z<X,n>,A,B>:tag<Z<subst_t<X,A,B>,n>>{};
template<class R,class...Xs, class A, class B>
struct subst<R(Xs...),A,B>:tag<subst_t<R,A,B>(subst_t<Xs,A,B>...)>{};
現在我們用它:
struct own_type {};
template<class Sig>
struct recursive_func{
using func=std::function< subst_t<Sig, own_type, recursive_func> >;
template<class...Ts>
std::result_of_t<func const&(Ts...)>
operator()(Ts&&...ts)const{
return f(std::forward<Ts>(ts)...);
}
operator func const&()const&{return f;}
operator func&()&{return f;}
operator func()&&{return std::move(f);}
template<class F,
class=std::enable_if_t<
!std::is_same<recursive_func,std::decay_t<F>>::value
&& std::is_convertible<F,func>::value
>
>
recursive_func(F&&fin):f(fin){}
func* operator->(){return f;}
func const* operator->()const{return f;}
private:
func f;
};
這給你一個可愛的語法:
recursive_func< std::vector<own_type>() > f;
是一個返回自己類型的向量的函數。
這使用了一些C ++ 14 _t
別名。 如果你的編譯器是嚴格的C ++ 11, std::blah_t<?>
可以用typename std::blah<?>::type
替換。 可能還有其他錯別字。
缺點是,當使用own_type
時,期望其類型滿足直接上下文中的某些屬性的模板可能會失敗。 這可以固定與延遲模板敷貼subst
理解,但在不經意的使用是不太可能是一個問題。
typedef std::function< callback_t(int &) > callback_t ;
**********
您嘗試使用定義本身定義新類型。 這是不可能的! 在定義之前不能使用callback_t。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.