Search code examples
c++clangc++23gcc14

Why does std::println with STL containers not compile with GCC and Clang?


Take a look at this snippet:

// test.cpp
#include <print>
#include <vector>

int main () {
    std::vector<int> vec = {1,2,3};
    std::println("vector: {}",vec);

    return 0;
}

From here I understood that the C++23 feature "support for the formatted output library " (P2093R14) had been added in GCC 14, Clang 18 and MSVC 19.37.

Compiled with MSVC 19.42 the program yields the desired ouput:

cl /EHs test.cpp /std:c++latest

D:\>test.exe vector: [1, 2, 3]

But with GCC 14 and Clang 19 it does not compile.

clang++ test.cpp -std=c++23

/usr/lib/gcc/x86_64-linux-gnu/14/../../../../include/c++/14/format:4065:3: error: 
static assertion failed due to requirement 
'is_default_constructible_v<std::formatter<std::vector<int, std::allocator<int>>, 
char>>': <plenty of more lines to follow>

g++ test.cpp -std=c++23

/usr/include/c++/14/format:4065:10: error: static assertion failed:
std::formatter must be specialized for each type being formatted
(is_default_constructible_v<formatter<_Args, _CharT>> && ...)

Am I missing something?


Solution

  • Formatter for std::vector is a part of P2286R8 "Formatting Ranges" and is not yet supported by libstdc++. (Both GCC and Clang use libstdc++ as the default standard library implementation on Linux.)