C++11变量函数类型模板参数

C++11 variadic function type template parameters

本文关键字:参数 类型 变量 函数 C++11      更新时间:2023-10-16

可能重复:
"…hellip”代币

有一种相对较新的方法可以直接指定函数类型(至少作为模板参数)。我不知道这是否是严格意义上的C++11,但我在阅读GCC 4.7的STL头文件时发现了它。

它是这样的:

std::function<void(int, char**)> f;

现在,在头文件<functional>中,我看到以下内容:

template <typename R, typename... A>
struct SomeStruct<R(A...)> { /* */ };

这是可以理解的:SomeStruct对于返回类型为R和参数类型为A的函数类型的显式专门化。

但考虑一下这个声明(在下一行):

template <typename R, typename... A>
struct SomeStruct<R(A......)> { /* */ };

那个双省略号是什么意思?

我个人觉得不清楚,但如果你知道这些是等价的,那就更有意义了:

void example(int, char, ...); // C-style variadic arguments
void example(int, char...);   // equivalent: the comma before the ellipses is optional

因此,当函数采用形式时,专业化只是涵盖了这种情况

R(A..., ...)

如在sprintf中:RintA...char*const char*,并且是C型变异体。