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 <tymi.cpp@gmail.com>

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 compliant functions for it. std::wprint and std::wprintln namely.

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 function templates to the standard library.

Technical Specification

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

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

Implementation Experience

ECPPS has experimental support for std::wprint/std::wprintln via a language extension.

Design Decisions

In an ideal world, the std::print/std::println functions would support overloads for std::wformat_string. Sadly they do not. An alternative approach to std::wprint/std::wprintln would be to add such overloads. However, I find it inconsistent with other library features such as std::(w)cout, std::(w)string, std::(w)format_string itself etc. Except for few rare cases (such as std::format function template), std::wmeow is the standard preferred version.

There could also exist std::u8print, std::u16print, std::u32print functions, but this revision does not include them. Maybe in further revisions or another proposal.

References

Appendix A. Examples

Here are a couple of examples where std::wprint/std::wprintln 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::wprintln(L"The System Locale is {}", locale);
}

Expected output:

The System Locale is en-GB