-2

I have a Visual Studio 2019 C++ project that has this definition in one header file:

static const PWSTR s_rgComboBoxStrings[] =
{
    L"First",
    L"Second",
    L"Third",
};

In that case, this error is shown:

a value of type "const wchar_t *" cannot be used to initialize an entity of type "const PWSTR"

Why is that? if I use PCWSTR instead of const PWSTR it compiles, but the problem is that I have the same problem in Windows SDK .h files so it not a good idea to modify windows .h files.

Curiously, I have other project that has exact the same definition (since I just copied and pasted the code), and that project compiles perfectly.

I have compared the project settings but it seems they are all the same.

I have thought about copied all the project files and then rename the files and change it according to my own project requirements, but I want to find out what is the problem with this project first.

Regards Jaime

jstuardo
  • 3,901
  • 14
  • 61
  • 136
  • 2
    Maybe helpful [What is the difference between const int*, const int * const, and int const *?](https://stackoverflow.com/q/1143262/10871073) – Adrian Mole Dec 22 '19 at 19:37
  • @AdrianMole nope... I know the difference. In fact, `PCWSTR` is defined as `CONST WCHAR *PCWSTR`, while `PWSTR` is `WCHAR *`. So, definition is equivalent when using `const PWSTR`. My theory is that some project setting is affecting. As I told, the other project, which has the same code, works. – jstuardo Dec 22 '19 at 19:49
  • For your code in VS2019 I get: "error C2440: 'initializing': cannot convert from 'const wchar_t [6]' to 'const PWSTR'" followed by "message : Conversion from string literal loses const qualifier (see /Zc:strictStrings)" So maybe check your project's `/Zc` settings. (PS Your 'fix' kills the warning in my IDE, also!) – Adrian Mole Dec 22 '19 at 19:53
  • 3
    `const WCHAR *` != `const PWSTR` - reread link from first comment – RbMm Dec 22 '19 at 20:00
  • 1
    `s_rgComboBoxStrings` in your code is `const` array to `WCHAR*` but not array of `const WCHAR*` – RbMm Dec 22 '19 at 20:05
  • And finally, comparing with the project where it does work, I realized that my project had ´/permissive-´ option set. I removed that, and code compiled. – jstuardo Dec 22 '19 at 20:28
  • @jstuardo - this not related to ´/permissive-´ direct. `const PWSTR` is really `WCHAR * const` – RbMm Dec 22 '19 at 20:30
  • @RbMm maybe, but that project, taken from Windows SDK samples (Credential Provider) has that settting. – jstuardo Dec 22 '19 at 20:31
  • 1
    `const PWSTR` is not equivalent to `PCWSTR` at all – user7860670 Dec 22 '19 at 20:32

1 Answers1

2

assume we heave

typedef struct T * PT;

in this case const PT != const T* but const PT == T* const

can be visible in this example

struct T 
{
    void operator++();
};
typedef struct T * PT;

void fn (const T* q)
{
    const PT p = q;// (1) Conversion loses qualifiers
    p++;           // (2) you cannot assign to a variable that is const
    *p = *q;       // (3) ok
}
RbMm
  • 31,280
  • 3
  • 35
  • 56