Ok, seems to be not so easy.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
#include <iostream>
template <typename T>
T *talloc(size_t s){
T *r=new T[s];
memset(r,0,s*sizeof(T));
return r;
}
using namespace std;
int main()
{
char *a = talloc((size_t)6); // talloc(6) produced error "no matching function for call to `talloc(int)'"
// talloc((size_t)6) produced error "no matching function for call to `talloc(size_t)'"
strncpy(a, "Hallo", 5);
cout << "a:" << a << ":a" << endl;
}
| |
Where exactly has a template to placed? Maybe I'm just displacing the template.
Hmm, T *r=new T[s] should be char *chrptr = new char[s] in runtime.
This would require T to be char.
If T is char then T *talloc would be char *talloc().
But wouldn't that require me to call something like this:
char a = talloc(5)
so type of T will be char? But then I would return a char* to a char?
I'm confused.
Ok, got it, implicite specialization isn't possible in this special case.
char *a = talloc<char>(6)
This did indeed work, as required. Thanks helios.
If I could rate you at this site I would give you a straight A+ for all the help you provide.