Template Magic为函数创建可变长度参数? [英] Template Magic to create variable length arguments to a function?

查看:78
本文介绍了Template Magic为函数创建可变长度参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我听说可以使用类+模板魔术来实现
制作独立功能(或者只是一个类似于

功能的仿函数)接受可变长度参数而不使用

elipse。例如,函数可以采用1种已知类型,然后是其他参数的变量列表:


ReturnValueClass rvc = someFunction(某些已知类型 ;,1,0.2,

" string");

ReturnValueClass rvc2 = someFunction("some some type again,true,0);


这没有定义someFunction与各种不同的类型

。 Boost.Python使用他们的call进行此操作始终

的方法接受PyObject *作为第一个参数,具有

返回值的模板,以及第一个参数后的所有其他内容

神奇地接受了。这是调用它的示例:


int return1 = call< int>(somePyObject1,'a'',1,true);

double return2 = call< double>(somePyObject2,0.123,4.2);

bool return 3 = call< bool>(somePyObject3);


有谁知道我在哪里可以学习如何做到这一点?我已经开始通过Boost.Python源来学习,但这是一个模板,宏和调用的真正的泥潭。看起来它是通过拨打电话功能来完成的。一个类/结构,并且函数

参数通过在

模板中模板化+宏扩展而变得合法。除此之外,我无法弄清楚如何做到这一点(我的

使用这与boost.python正在做的非常类似,只有

其他的东西比起Python)。


我实际上并没有在这里寻找解释这个复杂主题的人

在团队中(尽管那会很棒)。我真的只是想知道

如果有人能把我链接到一个最小的例子/教程如何做到这一点

我可以为我自己的应用程序构建


谢谢......

I have heard that it is possible to use classes + template magic to
make standalone functions (or maybe just a functor which acts like a
function) which can accept variable length arguments without using the
elipse. For example, a funciton could take in 1 known type, then a
variable list of other parameters:

ReturnValueClass rvc = someFunction("Some known type", 1, 0.2,
"string");
ReturnValueClass rvc2 = someFunction("Some known type again", true, 0);

This is without defining someFunction with the various different types
involved. Boost.Python does this with their "call" method which always
takes in a PyObject * as the first parameter, has a template for the
return value, and everything else after the first parameter is
magically accepted. This is example calling it:

int return1 = call<int>(somePyObject1, ''a'', 1, true);
double return2 = call<double>(somePyObject2, 0.123, 4.2);
bool return 3 = call<bool>(somePyObject3);

Does anyone know where I can learn how to do this? I have started
digging through the Boost.Python source to learn, but that is a
veritable quagmire of templates, macros, and calls. It looks like it''s
done by making the call "function" a class/struct, and the function
paramaters are made legal by templating+a macro expansion in the
template. Beyond that I''m having trouble figure out how to do this (my
use for this is very similar to what boost.python is doing, only with
something other than Python).

I''m not actually looking for someone to explain this complex topic here
on the group (though that would be great). I really just want to know
if anyone can link me to a minimal example/tutorial of how to do this
that I can build on for my own application

Thanks...

推荐答案

注意我不能使用elipse因为我需要保留

类型的参数。我打算把所有的参数

传递给一个对象的构造函数,这个构造函数超级负载

来处理这些类型,然后将每个对象放入一个

std :: list / vector中,这样我就可以操纵它们了。


无论如何,这是个主意。

Note I cannot use the elipse for this because I need to preserve the
type of the parameters. I''m planning on taking all of the parameters
passing each one into an object''s constructor that''s super-overloaded
to handle those types, and then putting each of those objects into an
std::list/vector so that I can manipulate them.

That''s the idea anyway.


无法使用模板实现变量参数列表。


解决方案是为1提供重载你的

特定函数/函子的最大参数。


例如


模板< typename R,typename T1>

R x(T1 arg1);


模板< typename R,typename T1,typename T2>

R x(T1 arg1,T2 arg2);


模板< typename R,typename T1,typename T2,...,typename TN>

R x (T1 arg1,T2 arg2,...,TN argn);


你可以使用boost :: preprocessor以编程方式扩展你的
函数来自通用定义,这样就可以避免大量的冗余代码和可能的错误。你在

boost / python / call.hpp中看到的宏是boost :: preprocessor用法的一个例子。


- peter

it is not possible to implement variable argument lists with templates.

the solution is to provide overloads for 1 to max arguments for your
specific function/functor.

e.g.

template <typename R, typename T1>
R x(T1 arg1);

template <typename R, typename T1, typename T2>
R x(T1 arg1, T2 arg2);

template <typename R, typename T1, typename T2, ..., typename TN>
R x(T1 arg1, T2 arg2, ..., TN argn);

you can use boost::preprocessor to programatically expand your
functions from a generic definition, this way you avoid a lot of
redundant code and possibly bugs. the macros that you see in
boost/python/call.hpp are an example of boost::preprocessor usage.

-- peter


Cl ********* @ yahoo .com 写道:
我听说有可能使用类+模板魔术来制作独立的功能(或者可能只是一个像
function)它可以接受可变长度的参数而不使用
elipse。例如,函数可以采用1种已知类型,然后是其他参数的变量列表:

ReturnValueClass rvc = someFunction(Some some type,1,0.2,
" string");
ReturnValueClass rvc2 = someFunction("some some type again,true,0);

这没有定义someFunction与各种不同的类型返回值的模板,以及第一个参数被魔法接受后的其他所有东西。这是调用它的示例:

int return1 = call< int>(somePyObject1,'a'',1,true);
double return2 = call< double>(somePyObject2,0.123 ,4.2);
bool return 3 = call< bool>(somePyObject3);

有谁知道我在哪里可以学习如何做到这一点?我已经开始挖掘Boost.Python源代码来学习,但这是一个真正的模板,宏和调用的泥潭。看起来它是通过拨打电话功能来完成的。一个类/结构,并且函数
参数通过模板+
模板中的宏扩展而变得合法。除此之外,我无法弄清楚如何做到这一点(我的使用方法与boost.python正在做的非常类似,只有使用除Python以外的其他东西)。

我实际上并没有在这里找人解释这个复杂的话题
关于小组(虽然那会很棒)。我真的只是想知道
是否有人可以将我链接到一个最小的示例/教程如何做到这一点
我可以为我自己的应用程序构建

谢谢.. 。
I have heard that it is possible to use classes + template magic to
make standalone functions (or maybe just a functor which acts like a
function) which can accept variable length arguments without using the
elipse. For example, a funciton could take in 1 known type, then a
variable list of other parameters:

ReturnValueClass rvc = someFunction("Some known type", 1, 0.2,
"string");
ReturnValueClass rvc2 = someFunction("Some known type again", true, 0);

This is without defining someFunction with the various different types
involved. Boost.Python does this with their "call" method which always
takes in a PyObject * as the first parameter, has a template for the
return value, and everything else after the first parameter is
magically accepted. This is example calling it:

int return1 = call<int>(somePyObject1, ''a'', 1, true);
double return2 = call<double>(somePyObject2, 0.123, 4.2);
bool return 3 = call<bool>(somePyObject3);

Does anyone know where I can learn how to do this? I have started
digging through the Boost.Python source to learn, but that is a
veritable quagmire of templates, macros, and calls. It looks like it''s
done by making the call "function" a class/struct, and the function
paramaters are made legal by templating+a macro expansion in the
template. Beyond that I''m having trouble figure out how to do this (my
use for this is very similar to what boost.python is doing, only with
something other than Python).

I''m not actually looking for someone to explain this complex topic here
on the group (though that would be great). I really just want to know
if anyone can link me to a minimal example/tutorial of how to do this
that I can build on for my own application

Thanks...




有几种选择。请参阅Cy的这篇文章,作为一个例子:

http://groups.google.com/group/comp....becab933500acc


干杯! --M



There are several options. See this post by Cy for an example of one:

http://groups.google.com/group/comp....becab933500acc

Cheers! --M


这篇关于Template Magic为函数创建可变长度参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆