我试图推断一个非类型模板参数。
#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?
}
但我收到错误:
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>'
是否可以推断出无符号整数,而不必显式声明模板参数?
我想让它干净如:getsize(4U) 我想避免写:getsize<4U>()
非常感谢您的帮助