c++language-lawyerstandard-library

std::format formatter for volatile


This simple code:

#include <print>

int main()
{
    volatile int i = 5;
    std::println("{}", i);
}

Doesn't compile with any major implementation of the std library. Any particular reason why it doesn't have a formatter? Or is it just an omission?

Sample error:

error: static assertion failed due to requirement 'is_default_constructible_v<std::formatter<volatile int, char>>': std::formatter must be specialized for the type of each format arg


Solution

  • The current consensus in the Committee is to mostly ignore support for volatile in the library, see: P1831R1 Deprecating volatile: library.

    Excerpt from parent paper P1152R0 Deprecating volatile:

    We propose to deprecate, and eventually remove, volatile partial template specializations, overloads, or qualified member functions for all but the atomic and numeric_limits parts of the Library.

    In other words, apart from the intentionally volatile nature of atomic components, the standard library is happy not to care about volatile.