简短的回答是“模板不是字符串替换”。void f(void)
仅当它是别名时才有意义void f()
在C++中,为了向后兼容C。
第一步是使用可变参数,如其他地方所述。
第二步是弄清楚如何映射void
返回函数...好吧,也许类似std::function<void()>
,或者也许是其他东西。我说也许是别的东西,因为与其他情况不同,你不能打电话std::function<void()> foo; foo( []()->void {} );
——这不是真正的延续。
也许是这样的:
template<typename T>
struct Continuation
{
typedef std::function<void(T)> type;
};
template<>
struct Continuation<void>
{
typedef std::function<void()> type;
};
然后像这样使用它:
auto someFunc = []()->void {};
Continuation<decltype(someFunc())>::type c;
这会给你你想要的类型。您甚至可以添加 apply to 延续:
template<typename T>
struct Continuation
{
typedef std::function<void(T)> type;
template<typename func, typename... Args>
static void Apply( type const& cont, func&& f, Args... args)
{
cont( f(args...) );
}
};
template<>
struct Continuation<void>
{
typedef std::function<void()> type;
template<typename func, typename... Args>
static void Apply( type const& cont, func&& f, Args... args)
{
f(args...);
cont();
}
};
如果传入类型是 void 或非 void 类型,它允许您统一将延续应用于函数的执行。
但是,我会问“你为什么要这样做”?