Document Number: D3673R0
Date: 16.04.2025

std::wprint / std::wprintln

Document Number: D3673R0

Date: 16.04.2025

Audience: Library Evolution Working Group Incubator (LEWGI, SG18), SG16

Author(s): Tymi <[email protected]>

Abstract

C++ introduced std::print/std::println in C++23. While most functions support wide strings, this particular one lacks the support. This proposal aims to improve that by adding a collection of wide string overloads functions for it, accepting std::wformat_string.

Introduction

This proposal aims to improve the interface of the printing utilities by extending them with wide string versions.

That will allow for a better integration with systems using wide characters and programs preferring them.

Revision History

Motivation and Scope

C++23 has introduced std::print and std::println function templates, which allow for easy and efficient text output to the console. They provide an interface for regular string formats. However, they do not provide an interface for wide strings, which are commonly used in Windows systems, as well as in specialised systems that work with non-standard encoding. I aim to improve that, by adding an interface to make it accessible.

Impact on the Standard

New Functionality

Implementing this proposal means adding new overloads for a function template to the standard library.

Technical Specification

namespace std
{
     template <typename... Args>
     void print(wformat_string<Args...> fmt, Args&&... args);
     template <typename... Args>
     void print(FILE* stream, wformat_string<Args...> fmt, Args&&... args);
     template <typename... Args>
     void println(wformat_string<Args...> fmt, Args&&... args);
     template <typename... Args>
     void println();
     template <typename... Args>
     void println(FILE* stream, wformat_string<Args...> fmt, Args&&... args);
     template <typename... Args>
     void println(FILE* stream);
}

These are the possible signatures for the functions covered by this proposal.

Implementation Experience

No implementation experience yet, although there is a work in progress project to add those overloads into MSVC, clang and ECPPS.

Design Decisions

Some might say adding a separate function for wide string support such as wprint/wprintln would align more with other functions such as printf, cout, etc. Although in my opinion these names are remaments of C, and std::print is closer to std::format which uses the overload convention.

There could also exist overloads for other strings (std::basic_format_string >charN_t<), but this proposal does not cover those. Maybe in a future revision or a separate paper.

References

Appendix A. Examples

Here are a couple of examples where std::print/std::println wide string overloads could be benefitial.

WinAPI

int main(void)
{
     std::wstring locale{};
     locale.resize(LOCALE_NAME_MAX_LENGTH);
     locale.resize(GetUserDefaultLocaleName(locale.data(), locale.size()) - 1);     

     std::println(L"The System Locale is {}", locale);
}

Expected output:

The System Locale is en-GB

Unicode characters in paths

void PrintPath(std::filesystem::path path)
{
     std::println(L"Path = {}", path);
}