C++11变量函数类型模板参数
C++11 variadic function type template parameters
可能重复:
"…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
中:R
是int
,A...
是char*
和const char*
,并且是C型变异体。
相关文章:
- 扩展C++生成的代码的模板参数类型名称
- 如何在 c++ 中定义接受不同参数类型的函数向量?
- 在 C++ 中运行时调用模板时,是否可以切换模板的参数类型?
- 将函数参数类型声明为 auto
- 将函数的参数 - 签名从使用 'std::function<T>' 转换为模板参数类型
- 在 C++17 中调用具有不同参数类型的构造函数
- 具有先前参数类型匹配的参数包
- 我想知道为什么"std::unique_ptr<int> foo(新 int)"是合法的,因为"std::<int>unique_ptr"要求输入参数类型应该是"int"?
- 将可变参数类型列表的扩展打包为复杂类型的初始值设定项列表 - 合法吗?
- MSVC 错误:4 个重载中的任何一个都无法转换所有参数类型
- 使用constexpr + auto作为返回和参数类型的奇怪类型推导
- 如何从第一个参数推断第二个参数类型?
- C++模板函数中,指定回调函子/lambda 的参数类型,同时仍允许内联?
- 如何用不同的参数类型和数字回调函数
- C++stoi:这两个重载都无法转换所有参数类型
- 为什么std::{container}::template不能推导其参数类型
- 为模板参数类型中的新对象分配内存
- 为指向成员模板参数的指针推导额外模板参数类型的紧凑方式
- 使用std::conditional中的模板来确定函数参数类型
- C++中的短参数类型