Can compiler not deduce the return type of a function if the return type happens to be a template parameter? Or I am making mistake in following code.
#include <iostream>
template <typename TT>
TT retBoolFail(bool a) {
return true;
}
template <typename TT>
void retBoolSuccess(bool a, TT& ret) {
ret = true;
return;
}
int main() {
bool ret;
retBoolSuccess(true, ret); // Success
retBoolFail(true); // Failure
return 0;
}
line 'RetBoolFail(true)' fails with following error.
-*- mode: compilation; default-directory: "~/work/c++/tupleTemplate/" -*-
Compilation started at Thu Mar 21 16:52:50
g++ -c simpletemp.cc -std=c++1z -g; g++ -o simpletemp simpletemp.o
simpletemp.cc: In function ‘int main()’:
simpletemp.cc:17:21: error: no matching function for call to ‘retBoolFail(bool)’
retBoolFail(true); // Failure
^
simpletemp.cc:4:4: note: candidate: template<class TT> TT retBoolFail(bool)
TT retBoolFail(bool a) {
^
simpletemp.cc:4:4: note: template argument deduction/substitution failed:
simpletemp.cc:17:21: note: couldn't deduce template parameter ‘TT’
retBoolFail(true); // Failure
^
Compilation finished at Thu Mar 21 16:52:50
Thanks.
No, deduction only occurs within the parameter list. You can use auto instead or just make it a non-template function with a hard-coded return type.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With