Reputation: 3046
I've been trying just about anything I could think of to get the _CallWithRightmostArgsInner
function to properly fail so that SFINAE could work properly and with this attempt, VS2013 has given me the error:
error C2039: 'type' : is not a member of 'std::enable_if<false,void>'
Any ideas? Is there an alternative that would be better? The idea here is that I would like to make a function call to Function provided that Function takes a number or parameters as denoted by NumArgs. The last two variadic arguments should be forwarded to the function and the result returned.
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)...);
}
};
Upvotes: 2
Views: 838
Reputation: 10770
I got this working for g++-4.8 by changing your code to
#include <iostream>
template <class T>
struct function_traits
{
typedef void result_type;
};
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, class = typename std::enable_if<sizeof...(RightArgs) != NumArgs -1 >::type>
static result_type _CallWithRightmostArgsInner(const Function& call, LeftArg, RightArgs && ... rightArgs) {
return _CallWithRightmostArgsInner(call, std::forward<RightArgs>(rightArgs)...);
}
template <typename ... RightArgs, class = typename std::enable_if<sizeof...(RightArgs) == NumArgs>::type>
static result_type _CallWithRightmostArgsInner(const Function& call, RightArgs && ... rightArgs) {
return call(std::forward<RightArgs>(rightArgs)...);
}
};
void f(int i, int j)
{
std::cout << i << ' ' << j << std::endl;
}
int main()
{
SplitParameters<decltype(f), 2>::CallWithRightmostArgs(f, 1, 2, 3, 4);
}
The compiler didn't like you calling _CallWithRightmostArgs
from _CallWithRightmostArgsInner
, and I assumed you were actually trying to call the Inner
function.
g++ also didn't like converting 0
to void*
in the template parameter list so I changed that to just be class = enable_if<...>::type
instead.
I didn't look into the reasons it was failing in detail though, hopefully this is good enough for you.
EDIT:
With regards to the typename enable_if<...>::type* = 0
being rejected, I remembered that there is a similar issue with std::array
:
template <class T, int size>
void f(const std::array<T,size>&){}
This little snippet compiles just fine on its own, but when you do:
std::array<int,4> a;
f(a);
g++ gives:
test3.cpp: In function ‘int main()’:
test3.cpp:9:8: error: no matching function for call to ‘f(std::array<int, 4ul>&)’
f(a);
^
test3.cpp:9:8: note: candidate is:
test3.cpp:4:6: note: template<class T, int size> void f(const std::array<T, size>&)
void f(const std::array<T,size>&){}
^
test3.cpp:4:6: note: template argument deduction/substitution failed:
test3.cpp:9:8: note: mismatched types ‘int’ and ‘#‘integer_cst’ not supported by dump_type#<type error>’
f(a);
^
test3.cpp:9:8: note: ‘std::array<int, 4ul>’ is not derived from ‘const std::array<T, size>’
The problem, as it turns out, is I declared the template as taking an int
for the size
parameter, but what the compiler got was a std::size_t
which is not the same as an int
and even though you can convert between them easily.
In the example above, I can't even replace = 0
with = NULL
because that's just a 0L
literal, I would have to do = (void*)0
to get the compiler to accept it (because the default type of enable_if<true>::type
is void
).
Upvotes: 3