A templated 'strdup()'?
问题 template<typename T> static T *anydup(const T *src, size_t len) { T *ptr = malloc(len * sizeof(T)); memcpy(ptr, src, (len * sizeof(T))); return ptr; } Is this proper? Can I expect any errors from this when using an int, long, etc.? I'm very new to generic programming and am trying to learn more. 回答1: No this is not proper ! When you have a malloc() in C++ code, you should become very suspicious: malloc() allocates memory, but doesn't properly create objects. The only way to work with such