From: tohava on
First, for those that do not know Haskell, it suffices to say that
newtype is similar to C++ typedef with one difference. The difference
is that the new type is not just an alias for the oold type, but a
completely different type that just happens to share the semantics
(i.e. one cannot implicitly convert between the two types).


I was thinking about implementing something similar to Haskell's
newtype in C++. My idea was as following:
- For each primitive, we create an encapsulator class acting just like
it (i.e. for int, we have class Int, for pointer to something we have
Pointer<something>).
- We build a mapper class Mapper such that Mapper<T> for a primitive
returns it's encapsulator, and otherwise returns T itself.
- We have a define STRONG_TYPEDEF(oldtype, newtype) which does
something like class newtype : public Mapper<oldtype> {} (and can also
b e defined to be redefined to an ordinary typedef if we wish better
optimization at some point).

Several questions:
1) One flaw of this idea is that the semantics are slightly different
from Haskell's newtype (newtype ISA oldtype is true in our case). This
is not a big deal since for STRONG_TYPEDEF(int, Foo); STRONG_TYPEDEF
(int, Bar); we still have that Foo and Bar are different integral
types.
2) Can you think of other problems in my idea and/or improvements to
offer?

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: ng2010 on

"tohava" <tohava(a)gmail.com> wrote in message
news:43b83cb1-329f-4845-ae3e-af5c014bbb10(a)p24g2000yqm.googlegroups.com...
> First, for those that do not know Haskell, it suffices to say that
> newtype is similar to C++ typedef with one difference. The difference
> is that the new type is not just an alias for the oold type, but a
> completely different type that just happens to share the semantics
> (i.e. one cannot implicitly convert between the two types).

The Boost library already has a template class to do this.




--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]