A common way out is to specialize template on void* and then specialize T* and implement it as cast-only thin wrapper around void*. Then the code being duplicated is non-issue and type-safety while preserved by hand is in at least exactly one place (T* spec code). From there on everything is properly type-safe.