Class template with both pointer type and regular type
See this: C++ template specialization, calling methods on types that could be pointers or references unambiguously
The same technique should work here, allowing you to deal with the val
as a reference (or a pointer) uniformly in both cases.
CRTP may help reduce code duplication, allowing for common code for two specializations without any overhead, as well.
Note that ownership semantics get tricky when you sometimes use a pointer and sometimes an instance -- what is the lifetime of val
if sometimes it is a pointer of an argument, and other times it is a copy of the argument, and how to you enforce it?