Get aliased type

Jonathan M Davis newsgroup.d at jmdavisprog.com
Tue Jan 2 18:06:28 UTC 2018


On Tuesday, January 02, 2018 11:31:28 Steven Schveighoffer via Digitalmars-
d-learn wrote:
> On 1/2/18 7:45 AM, John Chapman wrote:
> > On Tuesday, 2 January 2018 at 12:19:19 UTC, David Nadlinger wrote:
> >> There is indeed no way to do this; as you say, aliases are just names
> >> for a particular reference to a symbol. Perhaps you don't actually
> >> need the names in your use case, though?
> >>
> >>  — David
> >
> > The idea was to distinguish between a BSTR (an alias for wchar* from
> > core.sys.windows.wtypes used widely with COM) and wchar* itself, chiefly
> > so that I could call the appropriate Windows SDK functions on them to
> > convert them to and from D strings. Although BSTRs look like wchar*s to
> > the end user they are not really interchangable - for example, calling
> > SysFreeString on a regular wchar* will cause a crash.
> >
> > According to the docs, a BSTR is prefixed with its length and ends in a
> > null character, but I'm not sure if checking for the existence of those
> > is going to be good enough
>
> Hm... perhaps the correct path is to define a BSTR as a struct with a
> wchar in it. I wouldn't even use Typedef since they aren't the same
> thing (one doesn't convert to the other).
>
> But I don't know how that would affect existing code.

Well, regardless, if he wants to treat BSTR and wchar* as different, he's
going to need to use different types for them, because aliases are literally
just for the programmer's benefit and are not distinguishable via any kind
of traits. So, ultimately, he either needs separate types, or he's going to
have to leave it up to the programmer to deal with the differences
themselves.

- Jonathan M Davis




More information about the Digitalmars-d-learn mailing list