我一直在尝试任何我能想到的让_CallWithRightmostArgsInner
函数正确失败的方法,以便 SFINAE 可以正常工作,通过这次尝试,VS2013 给了我错误:
error C2039: 'type' : is not a member of 'std::enable_if<false,void>'
有任何想法吗?有没有更好的替代方案?这里的想法是,我想对 Function 进行函数调用,前提是 Function 采用 NumArgs 表示的数字或参数。最后两个可变参数应转发给函数并返回结果。
template <typename Function, int NumArgs>
class SplitParameters {
public:
typedef typename function_traits<Function>::result_type result_type;
template <typename ... RightArgs>
static result_type CallWithRightmostArgs(const Function& call, RightArgs && ... rightArgs) {
static_assert(sizeof...(RightArgs) >= NumArgs, "Unable to make function call with fewer than minimum arguments.");
return _CallWithRightmostArgs(call, std::forward<RightArgs>(rightArgs)...);
}
private:
template <typename ... RightArgs>
static result_type _CallWithRightmostArgs(const Function& call, RightArgs && ... rightArgs) {
return _CallWithRightmostArgsInner(call, std::forward<RightArgs>(rightArgs)...);
}
// note the '==' vs '!=' in these two functions. I would assume that only one could exist
template <typename LeftArg, typename ... RightArgs, typename std::enable_if<sizeof...(RightArgs) != NumArgs>::type* = 0>
static result_type _CallWithRightmostArgsInner(const Function& call, LeftArg, RightArgs && ... rightArgs) {
return _CallWithRightmostArgs(call, std::forward<RightArgs>(rightArgs)...);
}
template <typename LeftArg, typename ... RightArgs, typename std::enable_if<sizeof...(RightArgs) == NumArgs>::type* = 0>
static result_type _CallWithRightmostArgsInner(const Function& call, LeftArg, RightArgs && ... rightArgs) {
return call(std::forward<RightArgs>(rightArgs)...);
}
};