VC++ 2022 (C++17) Compile Errors

I’m getting the following errors when compiling a C++ app in Visual Studio 2022 (C++ 17 standard). Note that this is just a small excerpt; many other related or similar errors follow.

1>------ Build started: Project: Jeweligami.BackOffice, Configuration: Debug x64 ------
1>ImageProcessor.cpp
1>F:\$oftware\Framework\OpenSource\OpenCV\build\include\opencv2\core\cvstd.hpp(135,51): error C2760: syntax error: 'static_cast' was unexpected here; expected 'type specifier'
1>(compiling source file '/ImageProcessor.cpp')
1>F:\$oftware\Framework\OpenSource\OpenCV\build\include\opencv2\core\cvstd.hpp(135,51):
1>the template instantiation context (the oldest one first) is
1>	F:\$oftware\Framework\OpenSource\OpenCV\build\include\opencv2\core\cvstd.hpp(139,2):
1>	see reference to class template instantiation 'cv::Allocator<_Tp>' being compiled
1>F:\$oftware\Framework\OpenSource\OpenCV\build\include\opencv2\core\cvstd.hpp(135,51): error C2760: syntax error: 'static_cast' was unexpected here; expected ')'

Here’s a snippet of cvstd.hpp (in OpenCV\build\include\opencv2\core), around the area where the error was flagged (line 135, char 51):

Also, I’m not building static libraries, but rather, I’m trying to use the pre-built DLLs - I’ve done the minimal installation required, i.e. configuring OpenCV_DIR environment setting, as well as specifying the OpenCV\build\include in my INCLUDE path.

I’ve never encountered this error in VC++ before; wondering if anyone would know what setting(s) I need to set/unset in order to resolve it. Unfortunately, I can’t drop to a lower standard (like 11) if that’s what’s needed - although the lowest standard available in my platform is 14, the app actually requires at least 17.

Regards,
-JL

Hopefully you managed to find a solution to this issue, but I thought I’d post in case anyone else has the issue and this solution works for them.

We just came across what looks like exactly the same problem - what was compiling fine in VS2019 with a debug build failed with the same error message in VS2022.

We tracked the problem down to the use of the DEBUG_NEW macro replacement which we have on for tracing memory leaks in debug builds, using the classic

#ifdef _DEBUG
#define DBG_NEW new ( _NORMAL_BLOCK , _FILE_ , _LINE_ )
#else
#define DBG_NEW new
#endif

If this definition is used before including the OpenCV headers, the VS2022 compiler doesn’t like it. (Intel 2023 compiler has no issues with it.)
Solutions are either remove that definition, or ensure that the definition is created after the inclusion of OpenCV headers.

HTH