0

In the midsts of moving to C++20 for a Win32 project in VS 2019. We have code that looks like:

m_wndWindow.Create(m_hwnd, CRect(0,0, 100, 100), ....);

This used to work, but now we're getting C2664 errors stating that CRect cannot be converted to ATL::_U_RECT. We've found that the error goes away if we change to the following:

auto rec = CRect(0,0,100,100);
m_wndWindow.Create(m_hwnd, rec, .....);

or

m_wndWindow.Create(m_hwnd, (LPRECT)CRect(0,0, 100, 100), ....);

It makes no sense to me that the first one works, though the second one makes sense because CRect has a user defined converter to LPRECT.

I'm not sure if this is due to a change how parameters are initialized in c++20, or if there is an issue with VS 2019.

We haven't upgraded to VS 2022, and I'm wondering if this is fixed, I'll wait till we upgrade to 2022 before trying to move to C++20.

Am I missing something?

bpeikes
  • 3,495
  • 9
  • 42
  • 80
  • I think this is due to trying to pass a non-const reference to a temporary. When you create a separate variable, `_U_RECT`'s constructor gets a reference to that variable. When you explicitly cast via `(LPRECT)`, its constructor gets a pointer instead (not a reference; there are two separate constructors). – heap underrun Mar 02 '23 at 05:46
  • 4
    With C++20, msvc by default enables `/permissive-`, which makes the compiler more standard conform. Passing a temporary to a lvalue ref is forbidden by the standard, but msvc allowed it by default in the past. – Sedenion Mar 02 '23 at 06:17
  • Please show the full error message, it likely contains the exact reason for the problem – Alan Birtles Mar 02 '23 at 07:29
  • *Am I missing something?* -- [See this](https://stackoverflow.com/questions/16380966/non-const-reference-bound-to-temporary-visual-studio-bug). – PaulMcKenzie Mar 02 '23 at 08:30
  • I have encountered it once in VS2022, /permissive - this option disables permissive behavior, in IDE, this option also makes IntelliSense engine underline non-compliant code. You could disable it in Conformance mode. – Yujian Yao - MSFT Mar 02 '23 at 08:30

0 Answers0