具有不同数量参数的函数 [英] Functions with varying number of arguments

查看:54
本文介绍了具有不同数量参数的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!


有没有人知道关于函数的类型安全实现的事情

有多少个参数?

我知道一个旧的解决方案 - elipsis(stdarg,h)。但它不是类型安全的!


谢谢!

解决方案

* di ** @ inotech.ru


是否有人对类型安全的函数实现有所了解<有不同数量的论点吗?
我知道一个旧的解决方案 - 省略号(stdarg,h)。但它不是类型安全的!




一切都取决于。


考虑std :: cout。它没有使用任何魔法(嗯,确实如此,对于

初始化,但不是用于参数传递!)。然而它支持

任意数量的不同类型的参数。


首先,最基本的,简单地重载一个函数:

void foo(){}

void foo(int){}

void foo(int,std :: string){}


等等。


其次,支持无限数量的相同类型的参数,


void foo(FooArgs const& args){}


其中FooArgs是一个类(适当时添加访问限制)


struct FooArgs

{

FooArgs& operator()(int aValue)

{

myValues.push_back(aValue);

返回* this;

}


std :: vector< SharedPtr<值const *> >价值;

};


这样你就可以调用foo


foo(FooArgs()(1 )(2)(3));


或者您可以使用逗号运算符或例如<<操作员代替

函数调用操作符。


这是链接调用的基本思路,让每个操作员调用

返回对象的引用,以便可以再次将相同或其他运算符

应用于结果,这由std :: cout使用。


第三,只是将一堆任意参数传递给某些其他函数(尤其是构造函数),没有什么比宏更好:


#define FOO (args)SomeOtherThing args


你会称之为


FOO((1,2,3));


还有其他技巧。


一切都取决于。

-

A :因为它弄乱了人们通常阅读文字的顺序。

问:为什么这么糟糕?

A:热门帖子。

问:usenet和电子邮件中最烦人的事情是什么?


* Alf P. Steinbach:


std :: vector< SharedPtr<值const *> >值;




显然我并不是故意在那里写SharedPtr ......那是

因为我打算草拟一些更通用的东西。其中我没有
,所以这应该只是一个std :: vector< int> ;.


抱歉。


-

答:因为它弄乱了人们通常阅读文字的顺序。

问:为什么这么糟糕?

A:热门发布。

问:usenet和电子邮件中最烦人的事情是什么?


< blockquote> Alf P. Steinbach写道:

* di**@inotech.ru
< blockquote class =post_quotes>
有没有人知道关于类型安全的函数实现有多少参数?
我知道一个旧解决方案 - elipsis(stdarg,h)。但它不是类型安全的!



一切都取决于。

考虑std :: cout。




std :: cout不是函数。


-


Pete Becker

Dinkumware有限公司( http://www.dinkumware.com

Hello All!

Does anybody know anything about type-safe implementation of functions
with varying number of arguments?
I know one old solution - elipsis (stdarg,h). But it is NOT type-safe!

Thanks!

解决方案

* di**@inotech.ru:


Does anybody know anything about type-safe implementation of functions
with varying number of arguments?
I know one old solution - elipsis (stdarg,h). But it is NOT type-safe!



It all depends.

Consider std::cout. It doesn''t use any magic (well, it does, for
initialization, but not for argument passing!). Yet it supports an
arbitrary number of arguments of different types.

First, the most basic, simply overload a function:

void foo() {}
void foo( int ) {}
void foo( int, std::string ) {}

and so on.

Second, supporting an unlimited number of arguments of the same type,

void foo( FooArgs const& args ) {}

where FooArgs is a class like (add access restrictions as appropriate)

struct FooArgs
{
FooArgs& operator()( int aValue )
{
myValues.push_back( aValue );
return *this;
}

std::vector< SharedPtr< Value const* > > values;
};

so that you could call foo like

foo( FooArgs()( 1 )( 2 )( 3 ) );

or you might use the comma operator or e.g. the << operator instead of
the function call operator.

That''s the basic idea of chaining calls, letting each operator call
return a reference to the object so that the same or some other operator
can be applied to the result again, which is used by std::cout.

Third, for just transferring an arbitrary bunch of arguments to some
other function (especially a constructor), nothing beats a macro:

#define FOO( args ) SomeOtherThing args

which you''d call like

FOO( (1, 2, 3) );

And there are other techniques.

It all depends.
--
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?


* Alf P. Steinbach:


std::vector< SharedPtr< Value const* > > values;



Obviously I didn''t mean to write SharedPtr... in there. That was
because I contemplated sketching something more general. Which I didn''t
do, so that should just be a std::vector<int>.

Sorry.

--
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?


Alf P. Steinbach wrote:

* di**@inotech.ru:


Does anybody know anything about type-safe implementation of functions
with varying number of arguments?
I know one old solution - elipsis (stdarg,h). But it is NOT type-safe!


It all depends.

Consider std::cout.



std::cout is not a function.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)


这篇关于具有不同数量参数的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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