Cpp20 non-strict char*

97 Views Asked by At

I've migrated our Visual Studio C++ project to Cpp20, and it's compiling fine except at a couple of places where some non-compliant library is called. The library defines macros like this:

#define IG_PDF_page_release_content(_hPage) \
   AM_COMM_FUNCTION_WRAPPER((AT_ERRCOUNT (CACCUAPI *)( LPCHAR, \
   HIG_PDF_PAGE)) \
   ,IG_comm_function_call)("PDF.IG_PDF_page_release_content", \
   (HIG_PDF_PAGE)(_hPage))

This causes strict-string violation errors at every call site, because the string "PDF.IG_PDF_page_release_content" being a const char* is passed to a char* argument. I could modify the library's header to get it to compile:

#define IG_PDF_page_release_content(_hPage) \
   AM_COMM_FUNCTION_WRAPPER((AT_ERRCOUNT (CACCUAPI *)( LPCHAR, \
   HIG_PDF_PAGE)) \
   ,IG_comm_function_call)((char *) "PDF.IG_PDF_page_release_content", \
   (HIG_PDF_PAGE)(_hPage))

However, this is ugly, because on the one hand, there are many, many such macros that need to be changed, and on the other hand, I'll have to redo that every time I want to upgrade the library to the latest version (assuming the library's manufacturer is going to continue to ignore the issue).

So instead, I'd like to tell the compiler to ignore the issue at each calling site. Unfortunately, C2440 is an error rather than a warning, so pragma warning does not seem to work. I tried:

#pragma warning(push, 1)
#pragma warning(disable : 2440) // doesn't work, this isn't a warning
char *s = "blah";
#pragma warning(pop)

#pragma warning(push, 1)
#pragma warning(disable : 4996) // related warning, but not the same as C2440
char *s = "blah";
#pragma warning(pop)

Any ideas?

1

There are 1 best solutions below

2
cpplearner On

I would strongly recommend asking the library owner to fix the issue.

That being said, MSVC offers the /Zc:strictStrings- option to revert to the non-conforming behavior.

AFAIK it is not possible to change this behavior in the middle of a translation unit, because this affects overload resolution.

int f(...);   // #1
int f(char*); // #2

int x = f("asd"); // With strict strings: calls #1. Without: calls #2