如何在C++中的函数中包含可变数量的参数.
C#中的模拟:
public void Foo(params int[] a) {
for (int i = 0; i < a.Length; i++)
Console.WriteLine(a[i]);
}
public void UseFoo() {
Foo();
Foo(1);
Foo(1, 2);
}
Run Code Online (Sandbox Code Playgroud)
Java中的模拟:
public void Foo(int... a) {
for (int i = 0; i < a.length; i++)
System.out.println(a[i]);
}
public void UseFoo() {
Foo();
Foo(1);
Foo(2);
}
Run Code Online (Sandbox Code Playgroud) 我想知道我是否可以使用由明确指定的单一类型组成的参数包.例如,像这样:
#include <iostream>
using namespace std;
void show() { }
template<typename First, typename... Rest>
void show(First f, Rest... rest)
{
cout << f << endl;
show(rest...);
}
void foo(int f, int... args) // error
{
show(f, args...);
}
int main()
{
foo(1, 2, 3);
}
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是定义foo().使用OS X clang ++版本5(llvm 3.3svn)我收到错误error: type 'int' of function parameter pack does not contain any unexpanded parameter packs.
当然,我可以通过更改foo()为函数模板来编译它:
template<typename... Args>
void foo(int f, Args... args)
{
show(f, args...); …Run Code Online (Sandbox Code Playgroud) 背景:我创建了以下类C,其构造函数应该采用N类型的变量B&:
class A;
class B
{
A* getA();
};
template<size_t N>
class C
{
public:
template<typename... Args>
inline C(Args&... args) :
member{args.getA()...}
{}
private:
std::array<A*, N> member;
};
Run Code Online (Sandbox Code Playgroud)
问题:我的问题是如何将variadic约束Args为所有类型B?
我的部分解决方案:我想定义一个谓词,如:
template <typename T, size_t N, typename... Args>
struct is_range_of :
std::true_type // if Args is N copies of T
std::false_type // otherwise
{};
Run Code Online (Sandbox Code Playgroud)
并相应地重新定义我的构造函数:
template <typename... Args,
typename = typename std::enable_if<is_range_of_<B, N, Args...>::value>::type
>
inline C(Args&... args); …Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个函数,该函数可以采用相同类型的多个参数,并作为模板传入。参数的数量在编译时是已知的:
struct Foo
{
int a, b, c;
};
template <uint32_t argsCount, typename T>
void fun(T ...args) // max number of args == argsCount
{
// ...
// std::array<T, argsCount>{ args... };
}
int main()
{
fun<3, Foo>( { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } );
// Dont want to do:
// fun( Foo{}, Foo{}, Foo{} );
// nor:
// fun<Foo, Foo, Foo>( ... );
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我必须考虑这些限制:
我希望能够做到以下几点:
#include <array>
struct blah { };
template<typename... Args>
constexpr auto foo(Args&&... args)
{
return std::array<blah, sizeof...(Args)>{{ args... }};
}
auto res = foo({}, {});
Run Code Online (Sandbox Code Playgroud)
以下答案并不令人满意:他们只想检查参数包是否为单一类型,但我想在参数中将值正确转换为它(否则它不起作用).
为使用数组,向量,结构等传递给variadic函数或可变参数模板函数的所有参数指定一种类型?
我也不能使用initializer_list,因为我无法计算传递给该array类型的参数数量.我特别不想打字foo(blah{}, blah{});.
我的可能性是什么?
假设我有一个可变参数模板类.如何创建一个函数,使其参数属于集合类型,例如int,参数的数量等于模板类型的数量?
template <typename... Types>
class Test
{
public:
void Func(???); // I don't know how to declare such a function
}
Test<string, bool, long> myTest; // Three types
myTest.Func(905, 36, 123315); // Three arguments, but always of type int.
Run Code Online (Sandbox Code Playgroud)
最后,该函数的目标是返回所提供的int的元组.为简单起见,我在示例代码中将函数显示为void.
我已经阅读了一些关于这个新的C++ 11功能的文章,但是我并不了解所有的东西(我是C++的新手).如何在C中使用va_argfrom stdarg.h来访问特定的参数?
template <typename ... Args>
void f(Args ... args)
{
for(size_t i = 0; i < sizeof ...(args); i++)
{
// obviously, args...[i] didn't work...
}
}
Run Code Online (Sandbox Code Playgroud) 我得到了一个简单的可变参数模板声明,就像经典的一样:
template <typename... Arguments>
class VariadicTemplate;
Run Code Online (Sandbox Code Playgroud)
我需要实现的是让VariadicTemplate类执行某些类型检查; 可变参数模板应以某种迭代形式检查接收到的所有参数应该说明类型<Foo>.
我在某个地方看到了类似的东西,但现在我无法辨认它在哪里:P