Sto cercando di capire come utilizzare C++ (11) <type_traits>
.Tipo di argomento argomento Deduzione non riuscita con C++ 11 <type_traits>
Ecco il mio programma di test banale
#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;
}
Quando compilato con GCC 4.8.1 errori come
/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);
^
non ho idea perché GCC non può dedurre il parametro di template U
. Qualcuno sa quali informazioni manca il mio codice, cioè come scrivo un programma in C++ 11 che accetta un tipo integrale senza segno come primo argomento e un tipo integrale firmato come secondo?
Non puoi decuce tipi sinistro di una qualificazione '::'. –
** 14.8.2.5 Dedurre gli argomenti del modello da un tipo [temp.deduct.type] ** 5 I contesti non dedotti sono: - Il nome-nidificato-specificatore di un tipo che è stato specificato usando un qualifica-id. – TemplateRex
Grazie per tutti i bei commenti e le risposte! –