【发布时间】:2012-11-19 17:06:35
【问题描述】:
我有一个函数应该接受任何类型的参数。因此我使用模板。
template <typename T>
void Function(T Parameter);
函数调用函数。在我的实际应用程序中,有一个带有字符串键的回调映射,但这对于这个问题并不重要。回调必须是返回类型为 void 的函数指针,但可以是任何参数类型。
void* Callback;
template <typename T>
void Function(T Parameter)
{
(function<void(T))Callback(Parameter);
}
假设回调是正确的类型,这应该如下所示。
Function<int>(42);
// should result in...
(function<void(int))Callback(42);
但在某些情况下,我想将void 作为参数传递。
Function<void>(void);
// should result in...
(function<void(void)>Callback(void);
如您所见,我不需要提供任何内容或 void 作为参数。但我不能将void 作为参数传递。存在类型名不正确的错误。
如何将void 作为函数参数传递?
【问题讨论】:
-
可能有
boost,但如果你只写function<void()>会发生什么? -
你应该有一个可变参数模板,比如
template <typename ...Args> void Function(Args...)。我们几天前讨论过这个问题;到处搜索。 -
听起来像是模板专业化的工作。不幸的是,类外函数不支持它
-
@Bartek。如果模板类型无效,我现在怎么能使用你的行?
-
@icepack。该函数在一个类中。
标签: c++ function templates parameters void