0

I am curious if it is possible in c++ to "re-declare" a type under another name. For example, could you make some declaration of a type "MyVec" that is exactly the same as std::vector?

Inheritance almost works, but that is bound by the rules of inheritance so not all private members/methods will come along. I am referring to a carbon-copy declaration.

Aliasing won't work either, as I want these types to be viewed as separate by the static type system.

Ben Chislett
  • 437
  • 6
  • 11
  • Why do you want them to be viewed as separate types? Is there some actual goal you are trying to accomplish? – Cortex0101 Feb 17 '21 at 00:13
  • 1
    "not all private members/methods will come along" Why is that a problem? You can't use private members/methods anyways. – Justin Feb 17 '21 at 00:13
  • Nope, no goal. I was initally working on a host/device vector abstraction for CUDA where the host and device vectors are separate in the type system but have the same implementations. I've since found a better way to do that, and now I'm just curious if the language supports this sort of aliasing – Ben Chislett Feb 17 '21 at 00:16
  • Found this post that answers my question. https://stackoverflow.com/questions/50709440/generic-type-alias-which-are-incompatible-to-each-other have flagged as duplicate – Ben Chislett Feb 17 '21 at 00:22
  • Short answer, no. ODR Says you only get one definition, which is what defines the type. You either make a new type, with all that entails, or make an alias. – NathanOliver Feb 17 '21 at 00:22
  • @BenChislett I've gone ahead and closed it to the dupe since you are okay with that. – NathanOliver Feb 17 '21 at 00:23

0 Answers0