I'm trying to understand how to use C++(11) <type_traits>
.
Here's my trivial test program
#include <type_traits>
template<class U, class S>
inline U add(typename std::enable_if<std::is_unsigned<U>::value,U>::type a,
typename std::enable_if<std::is_signed <S>::value,S>::type b)
{
return a + b;
}
int main(int argc, const char * argv[], const char * envp[])
{
unsigned int ui;
int i;
auto a = add(ui, i);
return 0;
}
When compiled with GCC 4.8.1 it errors as
/home/per/f.cpp: In function ‘int main(int, const char**, const char**)’:
/home/per/f.cpp:15:23: error: no matching function for call to ‘add(unsigned int&, int&)’
auto a = add(ui, i);
^
/home/per/f.cpp:15:23: note: candidate is:
/home/per/f.cpp:5:10: note: template<class U, class S> U add(typename std::enable_if<std::is_unsigned<U>::value, U>::type, typename std::enable_if<std::is_signed<S>::value, S>::type)
inline U add(typename std::enable_if<std::is_unsigned<U>::value,U>::type a,
^
/home/per/f.cpp:5:10: note: template argument deduction/substitution failed:
/home/per/f.cpp:15:23: note: couldn't deduce template parameter ‘U’
auto a = add(ui, i);
^
I have no clue why GCC can't deduce the template parameter U
. Anybody knows what information my code is missing, that is how I write a program in C++11 that takes a unsigned integral type as first argument and signed integral type as second?