Share via

Using variadic parameters in MFC / C++20 application

ChuckieAJ 371 Reputation points
2026-06-07T17:58:31.1766667+00:00

I have a MFC application, but it is configured to build with the C++ 20 standard. Where useful I try to use the newer constructs.

Today, I decided to try and implement some variadic functions. This is what I have so far:


    void CBaseCsvImporter::LogInfo(const CString& msg)
    {
        m_dlgLog.AddLogEntry(msg, true);
    }

    void CBaseCsvImporter::LogSkip(const CString& msg)
    {
        m_dlgLog.AddLogEntry(msg, true);
        ++m_skippedCount;
    }

    void CBaseCsvImporter::LogError(const CString& msg)
    {
        m_dlgLog.AddLogEntry(msg, true);
        m_dlgLog.SetErrorMode(true);
        ++m_errorCount;
    }

    void CBaseCsvImporter::LogInfo(const wchar_t* fmt, ...)
    {
        CString msg;
        va_list args;
        va_start(args, fmt);
        msg.FormatV(fmt, args);
        va_end(args);

        LogInfo(msg); // calls the existing CString overload
    }

    void CBaseCsvImporter::LogSkip(const wchar_t* fmt, ...)
    {
        CString msg;
        va_list args;
        va_start(args, fmt);
        msg.FormatV(fmt, args);
        va_end(args);

        LogSkip(msg);
    }

    void CBaseCsvImporter::LogError(const wchar_t* fmt, ...)
    {
        CString msg;
        va_list args;
        va_start(args, fmt);
        msg.FormatV(fmt, args);
        va_end(args);

        LogError(msg);
    }

Understandably the Visual Studio IDE started showing code analysis warnings, discouraging me from using this approach:

Snapshot of code analysis

I decided to ask Copilot / ChatGPT about this and it prosed using these functions in the header instead:

template<typename... Args>
void LogInfo(std::wstring_view fmt, Args&&... args)
{
    std::wstring msg = std::vformat(fmt, std::make_wformat_args(args...));
    LogInfo(CString(msg.c_str()));
}

AI says that I would need to adjust my code to call it like this:

LogSkip(L"Skipped line {}: duplicate publisher '{}'", csvLine, name);

Eventually the error messages will come from the string table as a resource. I would appreciate guidance on the best way forward to make use of the new way of doing the variadic functiosn, but keeping in mind that ultimately I use CString.

Windows development | Windows API - Win32
0 comments No comments

1 answer

Sort by: Most helpful
  1. Viorel 126.9K Reputation points
    2026-06-07T18:57:48.01+00:00

    Which errors do you mean?

    In order to make this work:

    CString s1 = L"text1";
    const wchar_t* s2 = L"text2";
    std::wstring s3 = L"text3";
    
    LogInfo( L"Info {}, {}, {}", s1, s2, s3 );
    

    this class can be added to header file (according to https://en.cppreference.com/cpp/utility/format/formatter):

    template< >
    struct std::formatter<CString, wchar_t> 
    {
        template<class ParseContext>
        constexpr ParseContext::iterator parse( ParseContext& ctx )
        {
            return ctx.end( );
        }
    
        template<class FmtContext>
        FmtContext::iterator format( CString s, FmtContext& ctx ) const
        {
            std::wostringstream out;
    
    		out << LPCWSTR(s);
    
            return std::ranges::copy( std::move( out ).str( ), ctx.out( ) ).out;
        }
    };
    

    Also add:

    #include <string>
    #include <string_view>
    #include <sstream>
    #include <format>
    

    Was this answer helpful?


Your answer

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