Im trying to deduce a non-type template argument.
#include <iostream>
template <unsigned int S>
void getsize(unsigned int s) { std::cout << s << std::endl; }
int main()
{
getsize(4U);
// Id like to do this without explicitly stating getsize<4U>(4);
// or even getsize<4U>();
// Is this possible?
}
But I am getting error:
deduce_szie_t.cpp: In function 'int main()':
deduce_szie_t.cpp:9:15: error: no matching function for call to 'getsize(unsigned int)'
deduce_szie_t.cpp:9:15: note: candidate is:
deduce_szie_t.cpp:4:6: note: template<unsigned int <anonymous> > void getsize(unsigned int)
deduce_szie_t.cpp:4:6: note: template argument deduction/substitution failed:
deduce_szie_t.cpp:9:15: note: couldn't deduce template parameter '<anonymous>'
Is it possible to deduce the unsigned int, without having to explicitly state the template parameter?
Id like to have it clean like: getsize(4U)
I want to avoid writing: getsize<4U>()
Thanks alot for any help
It is possible to deduce a non-type template argument from function arguments, but not in the way you want. It can only be deduced from the type of the function argument, not from the value.
For example: