元编程解决递归函数typedef? [英] metaprogramming solution to recursive function typedef ?
问题描述
typedef int foo(foo); // foo是一个指向函数的指针类型,它将另一个foo作为参数获得
另一个foo并返回一个int
我需要以某种方式实现上述效果。我试过的任何
编译器都不接受这个,即使我无法在标准中发现
限制它的任何东西。
有没有办法使用元编程实现这种类型的定义?我注意到boost :: variant做了一些元编程技巧,允许变体中包含
变体。但是我不清楚这是怎么回事?这个问题_may_与此不完全一样。
-Roshan Naik
typedef int foo ( foo ); // foo is a pointer-to-function type that takes
another foo as argument and returns an int
I need to achieve the above effect somehow. This is not accepted by any
compiler I have tried, even though I cant spot anything in the standard that
restricts it.
Is there a way to achieve this typedefintion using metaprogramming ? I
noticed that boost::variant does some metaprogramming tricks to allow
variants to be contained in variants. But it is unclear to me how that is
acheived, also that problem _may_ not be exactly the same as this.
-Roshan Naik
推荐答案
Roshan Naik写道:
Roshan Naik wrote:
typedef int foo(foo); // foo是一个指向函数的指针,它将另一个foo作为参数并返回一个int
我需要以某种方式实现上述效果。
typedef int foo ( foo ); // foo is a pointer-to-function type that takes
another foo as argument and returns an int
I need to achieve the above effect somehow.
你想解决的真正潜在问题是什么?
[snip]
Best
Kai-Uwe Bux
What is the real underlying problem that you are trying to solve?
[snip]
Best
Kai-Uwe Bux
* Roshan Naik:
* Roshan Naik:
typedef int foo(foo); // foo是一个指向函数的指针,它将另一个foo作为参数并返回一个int
我需要以某种方式实现上述效果。
typedef int foo ( foo ); // foo is a pointer-to-function type that takes
another foo as argument and returns an int
I need to achieve the above effect somehow.
struct Foo
{
virtual int operator()(Foo const *)const = 0;
} ;
然后如果你需要按值传递这些仿函数,你可以将它们包装在一个
仿函数中,并带有指向实际仿函数的智能指针。 br />
-
答:因为它弄乱了人们通常阅读文字的顺序。
问:为什么会这样是坏事吗?
A:热门发布。
问:usenet和电子邮件中最烦人的是什么?
struct Foo
{
virtual int operator()( Foo const* ) const = 0;
};
Then if you need to pass such functors by value, you can wrap them in a
functor with a smart-pointer to the actual functor.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Roshan Naik写道:
Roshan Naik wrote:
typedef int foo(foo); // foo是一个指向函数的指针,它将另一个foo作为参数并返回一个int
我需要以某种方式实现上述效果。我试过的任何编译器都不接受这个,即使我无法发现
限制它的标准中的任何内容。
有没有办法使用元编程实现这种类型的定义?我注意到boost :: variant做了一些元编程技巧,允许变体中包含变体。但是我不清楚它是如何实现的,而且问题_可能与此不完全相同。
typedef int foo ( foo ); // foo is a pointer-to-function type that takes
another foo as argument and returns an int
I need to achieve the above effect somehow. This is not accepted by any
compiler I have tried, even though I cant spot anything in the standard that
restricts it.
Is there a way to achieve this typedefintion using metaprogramming ? I
noticed that boost::variant does some metaprogramming tricks to allow
variants to be contained in variants. But it is unclear to me how that is
acheived, also that problem _may_ not be exactly the same as this.
也许你想要这些内容:
模板< class T>
struct CallFunctionPtr
{
typedef int(* functionPtr)(T);
typedef CallFunctionPtr< fPtr>下一个;
};
int main()
{
CallFunctionPtr< int(* )()> :: next :: functionPtr fPtr;
// fPtr的类型为int(*)(int(*)())
>
CallFunctionPtr< int(*)()> :: next :: next ::: functionPt r f2Ptr;
// f2Ptr的类型为int(*) (int(*)(int(*)()))
}
每个下一个内部类型对应于函数调用函数
,其指针作为参数传递。链接在一起的next的
的数量等于在嵌套函数指针耗尽之前将调用多少函数
。
格雷格
Perhaps you want something along these lines:
template <class T>
struct CallFunctionPtr
{
typedef int (*functionPtr)(T);
typedef CallFunctionPtr<fPtr> next;
};
int main()
{
CallFunctionPtr<int(*)()>::next::functionPtr fPtr;
// fPtr is of type int (*)(int (*)())
CallFunctionPtr<int(*)()>::next::next:::functionPt r f2Ptr;
// f2Ptr is of type int (*)(int (*)(int (*)()))
}
Each "next" inner type corresponds to a function call to a function
whose pointer is being passed as a parameter. The number of "next"''s
that are chained together equals how many functions will be called
before the nested function pointers are exhausted.
Greg
这篇关于元编程解决递归函数typedef?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!