Why does std::println(std::vector) fail to compile?
Asked Answered
R

1

26

I have the following code:

#include <print>
#include <vector>

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

Among the numerous errors this produces, there is (clang++ -std=c++23 -stdlib=libc++, https://godbolt.org/z/3z9Tseh37):

[...]/format_arg_store.h:167:17: error: static assertion failed due to [...]
  167 |   static_assert(__arg != __arg_t::__none, "the supplied type is not formattable");
      |                 ^~~~~~~~~~~~~~~~~~~~~~~~

This confuses me because according to the cppreference C++23 compiler support page, libc++ support std::println and implements P2286: Formatting Ranges.

Am I doing something wrong or is this a standard library bug?

Rage answered 4/1 at 0:44 Comment(0)
F
25

Am I doing something wrong or is this a standard library bug?

Formatting ranges are specified to be supported in <format>, but the standard does not specify that <print> must include <format>. (So just include <format> to make the code well-formed).

However, from a user-friendly perspective, including <print> should support formatting ranges.

There is already an open issue about this, see 71925.

Fields answered 4/1 at 0:57 Comment(3)
ran into this myself -- the error message is spectacularly unhelpful especially at a time where some compilers / libraries support print and some don't so it's easy to think that's what the problem is.Rosenzweig
just include <format> - which compiler will that work with? I tried g++-14 and it doesn't work. For some reason, godbolt doesn't work with std::print() and any compiler choice: godbolt.org/z/5MTrfKosdBetts
@PaulJurczak libstdc++ does not implement formatting ranges.Bothwell

© 2022 - 2024 — McMap. All rights reserved.