If T is a type, why can a normal function accept both T and const T but a specialized template function cannot? Does this mean the programmer has to type more code which defeats the purpose of using templates?
struct s
{
void func(const char *buf)
{
cout << "func(const char *)" << endl;
}
void func(const wchar_t *buf)
{
cout << "func(const wchar_t *)" << endl;
}
};
Output: Two different functions will be called for T and const T when T is char*/wchar_t*
func(const char *)
func(const wchar_t *)
func(const char *)
func(const wchar_t *)
The same but with templates:
struct ss
{
template<typename T>
void func (T t)
{
cout << "func (T)" << endl;
}
template<>
void func (const char *buf)
{
cout << "func (const char *)" << endl;
}
template<>
void func(const wchar_t *buf)
{
cout << "func (const wchar_t *)" << endl;
}
};
Output: Three different functions will be called for T and const T when T is char*/wchar_t*
func (const char *)
func (const wchar_t *)
func (T)
func (T)