warning C4830 "non-portable use of class 'ATL::CStringT<wchar_t, StrTraitMFC_DLL<wchar_t, ATL::ChTraitsCRT<wchar_t>>>' as an argument to a variadic function

2022-03-03T04:50:45.877+00:00

We were migrating our codebase from VS2015 to VS2022, we have some c++ components and updated them also to c++20 and then we encounter hundreds of warnings about C4830 "non-portable use of class 'ATL::CStringT<wchar_t, StrTraitMFC_DLL<wchar_t, ATL::ChTraitsCRT<wchar_t>>>' as an argument to a variadic function. We use a lot of CString in variadic functions such as String Format and for our own loggings and printings.

What is the best way to deal with this error

Developer technologies | C++
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Minxin Yu 13,506 Reputation points Microsoft External Staff
    2022-03-03T05:54:51.133+00:00

    Hi, @Rodriguez, Daniel C [AUTOSOL/RS/MNL]

    Have you read document: C4840. This warning is available beginning in Visual Studio 2017. The document also prodives samples and way to fix it:

    For strings built and managed using CStringW, the provided operator LPCWSTR() should be used to cast a CStringW object to the C-style string pointer expected by the format string:

     CStringW str1;  
     CStringW str2;  
      // ...  
     str1.Format("%s", static_cast<LPCWSTR>(str2));  
    

    Best regards,

    Minxin Yu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.