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和电子邮件中最烦人的事情是什么? 显然我并不是故意在那里写SharedPtr ......那是 因为我打算草拟一些更通用的东西。其中我没有,所以这应该只是一个std :: vector< int> ;. 抱歉。 - 答:因为它弄乱了人们通常阅读文字的顺序。 问:为什么这么糟糕? A:热门发布。 问:usenet和电子邮件中最烦人的事情是什么? Alf P. Steinbach写道: 一切都取决于。 考虑std :: cout。 std :: cout不是函数。 - Pete Becker Dinkumware有限公司( http://www.dinkumware.com ) Hello All!Does anybody know anything about type-safe implementation of functionswith varying number of arguments?I know one old solution - elipsis (stdarg,h). But it is NOT type-safe!Thanks! 解决方案It all depends.Consider std::cout. It doesn''t use any magic (well, it does, forinitialization, but not for argument passing!). Yet it supports anarbitrary 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 likefoo( FooArgs()( 1 )( 2 )( 3 ) );or you might use the comma operator or e.g. the << operator instead ofthe function call operator.That''s the basic idea of chaining calls, letting each operator callreturn a reference to the object so that the same or some other operatorcan be applied to the result again, which is used by std::cout.Third, for just transferring an arbitrary bunch of arguments to someother function (especially a constructor), nothing beats a macro:#define FOO( args ) SomeOtherThing argswhich you''d call likeFOO( (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?Obviously I didn''t mean to write SharedPtr... in there. That wasbecause I contemplated sketching something more general. Which I didn''tdo, 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? It all depends. Consider std::cout.std::cout is not a function.--Pete BeckerDinkumware, Ltd. (http://www.dinkumware.com) 这篇关于具有不同数量参数的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-11 16:00