【问题标题】:What kind of parameter unpacking is "... ..."? [duplicate]什么样的参数解包是“... ...”? [复制]
【发布时间】:2013-06-08 06:43:19
【问题描述】:

我在 libstdc++ (g++ 4.7.1) 中查看了<functional> 的来源,发现了以下代码 (l. 91 - 98):

/// Retrieve the result type for a function type.
template<typename _Res, typename... _ArgTypes>
struct _Weak_result_type_impl<_Res(_ArgTypes...)>    // (1)
{ typedef _Res result_type; };

template<typename _Res, typename... _ArgTypes>
struct _Weak_result_type_impl<_Res(_ArgTypes......)> // (2) line 97
{ typedef _Res result_type; };

虽然我知道通常的包扩展 ... (1) [temp.variadic],但我不知道 ...... 是什么。

...... (2) 在这种情况下是什么意思?

跟进问题

在下面的minimal example 中,TEST_PARAM_3 需要什么类型的tester&lt;TEST_PARAM_3&gt;::value == 2

#include <iostream>
#include <type_traits>
#include <iomanip>

template <typename Functor>
struct tester : std::integral_constant<int, 0>{};

template <typename Res, typename... Args>
struct tester<Res(Args...)> : std::integral_constant<int, 1>{};

template <typename Res, typename... Args>
struct tester<Res(Args......)> : std::integral_constant<int, 2>{};

#define STR_EXPAND(tok) #tok
#define STR(tok) STR_EXPAND(tok)
#define TEST_PARAM_1 void(*)(int, int)
#define TEST_PARAM_2 void(int, char, std::ostream&)
#define TEST_PARAM_3 void()

int main(){
    using std::setw;
    std::cout
    << setw(65)<< STR(TEST_PARAM_1) ": " << tester<TEST_PARAM_1>::value << "\n"
    << setw(65)<< STR(TEST_PARAM_2) ": " << tester<TEST_PARAM_2>::value << "\n"
    << setw(65)<< STR(TEST_PARAM_3) ": " << tester<TEST_PARAM_3>::value << "\n";
}

【问题讨论】:

  • 您的后续问题:void(...)
  • @Yuushi:确实。 ...... 似乎与搜索功能和搜索引擎的配合不太好。
  • @Zeta 我刚刚在 Google 中搜索了 C++ "six dots",它成功了:P
  • @Zeta 公平地说,它绝对是 C++ 的陌生角落之一。不久前我偶然发现了这个答案,所以它在我的记忆中还比较新鲜......

标签: c++ variadic-templates


【解决方案1】:

最后一个...实际上等价于, ...

这使得模板参数:

Res(Args..., ...)

这意味着这是一个variadic function,具有模板可变数量的固定参数。

【讨论】:

    猜你喜欢
    • 2021-11-16
    • 1970-01-01
    • 1970-01-01
    • 2013-01-22
    • 2019-06-07
    • 1970-01-01
    • 2014-07-29
    • 1970-01-01
    • 2019-01-14
    相关资源
    最近更新 更多