【问题标题】:g++ std::is_function implementation: what does _ArgTypes followed by 6 periods mean?g++ std::is_function 实现:_ArgTypes 后跟 6 个句点是什么意思?
【发布时间】:2013-02-02 08:12:47
【问题描述】:

我正在查看我的标头 (g++-4.5.2) 中的一些模板的实现,我发现了以下内容:

/// is_function
template<typename>
  struct is_function
  : public false_type { };
template<typename _Res, typename... _ArgTypes>
  struct is_function<_Res(_ArgTypes...)>
  : public true_type { };
template<typename _Res, typename... _ArgTypes>
  struct is_function<_Res(_ArgTypes......)>
  : public true_type { };

前两个声明似乎合理,但我不知道第三个是如何工作的。 ...... 是什么?我在标准里找了,没找到。

【问题讨论】:

  • 如果你想要 C++11,你应该至少使用 GCC 4.7,而不是 4.5(它在 C++2011 标准之前发布)
  • 相当于_ArgTypes..., ...
  • @BasileStarynkevitch:我实际上还没有编写 C++11 代码。我只是好奇。

标签: c++ templates c++11 g++ variadic-templates


【解决方案1】:

同理:

_Res(_ArgTypes..., ...)

省略号参数前的逗号是可选的。

【讨论】:

  • 这与_Res(_ArgTypes...) 有何不同?
  • @BrianBi:想象一下你将printf 传递给了这个。 _ArgTypes 将持有 const char*,并且需要最后的 ... 来匹配,因为没有类型代表它。
  • “省略号参数前的逗号是可选的” - 什么?惊人的! +1
  • 奇怪的是,它只是在 C++(不是 C)中是可选的。
【解决方案2】:

此模板处理函数中的可变长度参数。例如:

void foo(bool, char, int...)

【讨论】:

    猜你喜欢
    • 2021-12-19
    • 2020-04-26
    • 1970-01-01
    • 1970-01-01
    • 2015-02-20
    • 2015-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多