As the title asks, how do I make a copy of a arbitrary type, but is different from the original type? As in, they are not implicitly convertible to each other but have the same interface.
I'll take cuda's pointers as an example to explain why this might be needed. Since host(CPU) and device(GPU) memory are separate, there is exactly 0 chance where you'd want to dereference a device pointer in host code. However, both are just plain old pointers, the compiler won't complain if you did actually do that. This is where type checking naturally fits in, these are all compile-time knowledge.
But then, to create a new pointer type, I'd have to manually type all the pointer arithmetic into a class to achieve this.
template<typename T>
class dptr
{
//...
dptr& operator++();
//...
auto& operator*();
//...
bool operator==(dptr) const;
//...
operator bool() const;
private:
T* ptr;
};
And the boilerplate gets worse when the type to be copied is even more complicated and when noexcept
is sprinkled in.
What I'm looking for is basically what Go have: strong retyping of arbitrary types
type Int int
Int
is now an int
, with arithmetic and all that, but is not implicitly convertible to int
. (Actually, what I want is more like Go's type embedding, if the reader knows what that is.)