[英]Does C++ support variable numbers of parameters to functions?
Like printf
? 喜欢
printf
?
But I remember that C++ does name mangling with function name and parameter types, 但我记得C ++确实用函数名和参数类型命名,
this can deduce that C++ doesn't support variable length parameters... 这可以推断出C ++不支持可变长度参数......
I just want to make sure, is that the case? 我只是想确定,是这样的吗?
UPDATE UPDATE
the discussion should exclude those included by extern "C"
讨论应排除
extern "C"
所包含的内容
Yes. 是。 C++ inherits this from C. You can write:
C ++从C继承了这个。你可以写:
void f(...);
This function can take any number of arguments of any types. 此函数可以采用任何类型的任意数量的参数。 But that is not very C++-style.
但那不是很C ++风格。 Programmers usually avoid such coding.
程序员通常会避免这种编码。
However, there is an exception: in template programming, SFINAE makes use of this a lot. 但是,有一个例外:在模板编程中, SFINAE充分利用了这一点。 For example, see this (taken from here ):
例如,看到这个(取自这里 ):
template <typename T>
struct has_typedef_type {
// Variables "yes" and "no" are guaranteed to have different sizes,
// specifically sizeof(yes) == 1 and sizeof(no) == 2.
typedef char yes[1];
typedef char no[2];
template <typename C>
static yes& test(typename C::type*);
template <typename>
static no& test(...);
// If the "sizeof" the result of calling test<T>(0)
// would be equal to the sizeof(yes), the first overload worked
// and T has a nested type named type.
static const bool value = sizeof(test<T>(0)) == sizeof(yes);
};
This uses test(...)
which is a variadic function. 这使用
test(...)
,它是一个可变函数。
Yes, C++ supports the ellipsis from C, but I strongly advice against using them, since they are in no way type safe. 是的,C ++支持C语言的省略号,但我强烈反对使用它们,因为它们绝不是类型安全的。
If you have access to a good C++11 capable compiler with variadic template support, then you should use that instead. 如果您可以访问具有可变参数模板支持的优秀C ++ 11编译器,那么您应该使用它。 If you don't have that, have a look at how boost::format solves those things.
如果你没有,请看看boost :: format如何解决这些问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.