【问题标题】:C++11 variadic function type template parameters [duplicate]C ++ 11可变参数函数类型模板参数[重复]
【发布时间】:2012-07-10 17:35:15
【问题描述】:

可能重复:
What is the meaning of “… …” token?

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

是这样的:

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

现在,在头文件 &lt;functional&gt; 中,我看到以下内容:

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

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

但请考虑以下声明(在下一行):

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

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

【问题讨论】:

  • 啊,对。这是重复的,抱歉。

标签: c++ templates syntax c++11 std-function


【解决方案1】:

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

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

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

R(A..., ...)

如sprintf:R是int,A...是char*和const char*,是C风格的可变参数。

【讨论】:

    猜你喜欢
    • 2011-12-14
    • 1970-01-01
    • 2012-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多