0

I know that LPWSTR is a WCHAR * (from wtypes.h):

typedef WCHAR *LPWSTR;

but I can't find the definition for LPWSTR(s). Is a macro / a constructor / something else? Where is it defined?

In particular, are these two lines exactly equivalent?

LPWSTR a = (LPWSTR) b;   // cast
LPWSTR a = LPWSTR(b);

or does LPWSTR(...) do something else than a cast?

Basj
  • 41,386
  • 99
  • 383
  • 673
  • `LPWSTR(b)` is equivalent to `WCHAR*(b)`.. – Algirdas Preidžius Aug 04 '17 at 11:13
  • 1
    @DAle That's not what OP is asking about.. Did you read the question? – Algirdas Preidžius Aug 04 '17 at 11:14
  • @AlgirdasPreidžius I can't find the definition, where (in which .h file) is this defined, or maybe a MSDN link? – Basj Aug 04 '17 at 11:14
  • 1
    I'm tempted to say: Spare yourself all this winapi crap, just `#define UNICODE` everywhere and use `wchar_t` and `L`-prefixed literals with winapi calls, converting to and from UTF-8 as needed. But of course, this doesn't answer the question. –  Aug 04 '17 at 11:14
  • @Basj What definition do you want? There is none. Re-read my comment (keeping in mind that `LPWSTR` is a `typedef` to `WCHAR*`). – Algirdas Preidžius Aug 04 '17 at 11:15
  • That's not the question @FelixPalmen. I just would like to know how is defined LPWSTR(s) (not the typedef itself, for which I already know the definition). – Basj Aug 04 '17 at 11:15
  • 1
    the `(LPWSTR) b` and `LPWSTR(b)` and `reinterpret_cast(b)` is the same. this is only c++ specific cast – RbMm Aug 04 '17 at 11:16
  • @Basj I already said that. Just thinking it might be helpful to take a step back and realize what convoluted mess windows' handling of unicode has grown into ;) –  Aug 04 '17 at 11:17
  • @AlgirdasPreidžius But what is `WCHAR*(b)`? Is it the same as `(WCHAR*) b` ? – Basj Aug 04 '17 at 11:18
  • @RbMm is this true in general in C++? `(mytype) a` and `mytype(a)` are the same? – Basj Aug 04 '17 at 11:20
  • 2
    @Basj it's the "constructor style" syntax of a cast in C++ and invalid in C –  Aug 04 '17 at 11:20
  • Oh ok @FelixPalmen, did not know that. Is a constructor style cast exactly the same as a cast? – Basj Aug 04 '17 at 11:20
  • all this is exactly `reinterpret_cast` . this not generate any binary code. simply make compiler happy – RbMm Aug 04 '17 at 11:23
  • Ok so this is a general rule that I can remember in C++? `mytype(a)` (function-style cast) and `(mytype) a` (C-style cast) are exactly equivalent? – Basj Aug 04 '17 at 11:24
  • yes, this is equivalent. you can view for example assembly code, how this line converted, for understand that this is the same – RbMm Aug 04 '17 at 11:26
  • @Basj I think the issue would've been "more obvious" with only the C++ tag, so please add only one language tag in the future (with the known exceptions like e.g. interfacing one language to another of course). –  Aug 04 '17 at 11:32
  • Better use `LPTSTR` – i486 Aug 04 '17 at 11:56
  • @i486: What exactly is better about writing code, that *may* be compatible with Windows 95? In 2017. – IInspectable Aug 05 '17 at 01:08

1 Answers1

-3

If b is an instance of class B

LPWSTR a = (LPWSTR) b;   // call to operator B::LPWSTR()
LPWSTR a = LPWSTR(b);    //  call to B::operator()
  • 1. `LPWSTR` is a pointer. 2. Even if `LPWSTR` were a class, `LPWSTR(b)` would *not* be calling `operator()`, it would be calling a constructor. – jamesdlin Aug 04 '17 at 12:01