c++ – C2065 ‘PTRDIFF_MAX’ undeclared identifier


I am compiling my C++ project in VS-2022 community edition, the build failed with following repeated error (56 times),

Error   C2065   'PTRDIFF_MAX': undeclared identifier    
MyProjectName   D:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.39.33519\include\xlocnum   1032    

Error   C2065   'PTRDIFF_MAX': undeclared identifier    MyProjectName   D:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.39.33519\include\xlocnum   1027    

Error   C2065   'PTRDIFF_MAX': undeclared identifier    MyProjectName   D:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.39.33519\include\xlocnum   1028        

when i double click on error code to see the location in my source code it opens the file “xlocnum” at path: “D:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.39.33519\include”

Kindly guide to resolve this issue.
Thanks

I followed following two approaches to resolve the issues, but these not working for me either:

  1. Conditional Compilation (Recommended):

This approach lets you include the necessary headers based on the compiler version.

Add the following code to a frequently included header file (e.g., myproject_common.h) or directly in a central source file (e.g., main.cpp):

C++
#ifdef _MSC_VER  // Microsoft compiler
    #if _MSC_VER < 1900  // Prior to VS 2017 (version 19.0)
        #include <ciso624>  // For older compilers that don't have cstddef
    #else
        #include <cstddef>
    #endif
#else  // Other compilers (GCC, Clang, etc.)
    #include <cstddef>
#endif

This code checks the compiler version (_MSC_VER) and includes (pre-C++11) or (C++11+) accordingly.

  1. Define the Constant Manually (Less Preferred):

If conditional compilation isn’t feasible, explicitly define PTRDIFF_MAX with a suitable value (e.g., std::numeric_limits<ptrdiff_t>::max()) before including headers that might use it:

C++
#ifndef PTRDIFF_MAX
    #define PTRDIFF_MAX (std::numeric_limits<ptrdiff_t>::max())
#endif

#include <your_headers.h>
  1. I also installed Visual Studio 2022 Preview, but same errors appears in that too.

But if i tried to include above modification into xlocnum file it wont allow me to save this file, so that’s also failed.

If anyone know the cause, kindly answer to proceed for the resolution. Thanks beforehand

Build Logs:

D:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.39.33519\include\xlocnum(1027,38): error C2065: 'PTRDIFF_MAX': undeclared identifier
  (compiling source file 'Common.cpp')
  D:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.39.33519\include\xlocnum(1027,38):
  the template instantiation context (the oldest one first) is
    D:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.39.33519\include\xlocnum(1655,105):
    see reference to class template instantiation 'std::num_get<wchar_t,std::istreambuf_iterator<wchar_t,std::char_traits<wchar_t>>>' being compiled
    D:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.39.33519\include\xlocnum(588,38):
    while compiling class template member function '_InIt std::num_get<wchar_t,_InIt>::do_get(_InIt,_InIt,std::ios_base &,std::ios_base::iostate &,double &) const'
          with
          [
              _InIt=std::istreambuf_iterator<wchar_t,std::char_traits<wchar_t>>
          ]
        D:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.39.33519\include\xlocnum(622,16):
        see the first reference to 'std::num_get<wchar_t,std::istreambuf_iterator<wchar_t,std::char_traits<wchar_t>>>::do_get' in 'std::num_get<wchar_t,std::istreambuf_iterator<wchar_t,std::char_traits<wchar_t>>>::do_get'
    D:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.39.33519\include\xlocnum(593,13):
    see reference to function template instantiation 'std::_Num_get_parse_result std::num_get<wchar_t,std::istreambuf_iterator<wchar_t,std::char_traits<wchar_t>>>::_Parse_fp_with_locale<0>(char *const ,const int,_InIt &,_InIt &,const std::locale &)' being compiled
          with
          [
              _InIt=std::istreambuf_iterator<wchar_t,std::char_traits<wchar_t>>
          ]
  
D:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.39.33519\include\xlocnum(1028,43): error C2065: 'PTRDIFF_MAX': undeclared identifier
  (compiling source file 'Common.cpp')
  
D:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.39.33519\include\xlocnum(1029,60): error C2065: 'PTRDIFF_MAX': undeclared identifier
  (compiling source file 'Common.cpp')
  
D:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.39.33519\include\xlocnum(1032,38): error C2065: 'PTRDIFF_MAX': undeclared identifier
  (compiling source file 'Common.cpp')

Leave a Reply

Your email address will not be published. Required fields are marked *