Questions tagged [stdformat]

Tag for C++ std::format library specific questions

This tag should be used (along with the [C++] tag) for questions about std::format library.

15 questions
35
votes
1 answer

How to use C++20 in CMake project

I want to use the header available in C++20. I am using the most up-to-date release of CMake. My CMakeFiles looks like set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_BUILD_TYPE debug) set(CMAKE_CXX_FLAGS…
Tak Makoni
  • 467
  • 1
  • 4
  • 7
4
votes
1 answer

Exception message with C++20 format

Similar to the this question. Needed throw a exception with a message using printf-like style instead of string cocatenation or iostreams. Using the C++ 20 Formatting Library: throw std::runtime_error { std::format("Critical error! Code {}:…
Nico Engels
  • 407
  • 2
  • 12
4
votes
1 answer

is std::format at runtime possible?

Can I use a format string defined at runtime in a std::format? This seems to say that you can't; that all format strings are a compile-time-only thing. I could have sworn that I did this only a few months ago, maybe it was before the Standard was…
masher
  • 3,814
  • 4
  • 31
  • 35
3
votes
1 answer

No header file in /usr/include/c++/*

I want to use std::format from c++20. Installed g++12, libc++-12-dev, libstdc++-12-dev, but the header file is still missing. Ubuntu 22.04
krespo
  • 35
  • 2
3
votes
2 answers

std::format pointer error C3615: consteval function 'std::_Compile_time_parse_format_specs' cannot result in a constant expression

How to fix (stdC++20 mode VS2022) #include #include auto dump(int *p) { std::string resultstring = std::format(" p points to address {:p}", p); resulting in: error C3615: consteval function…
GeePokey
  • 159
  • 7
2
votes
1 answer

Inserting Characters in Placeholder in `std::format`

In the C++20 library, is it possible to insert characters in a placeholder, so that those characters behave like a part of the passed parameter? For example, I have the following statement: std::format("Test '{:<{}}'", 123, 5) which…
ENIAC
  • 813
  • 1
  • 8
  • 19
2
votes
1 answer

Is std::format going to work with things like ICU UnicodeString?

Rather than a long preamble, here is my core question, up front. The paragraphs below explain in more detail. Is there a template parameter in std::format (or fmt) that will allow me to format into ICU UnicodeStrings?, or perhaps into something like…
Rob N
  • 15,024
  • 17
  • 92
  • 165
2
votes
1 answer

Can I call std::format with a consteval std::array as the format string?

I have a consteval std::array that contains a format string: consteval std::array make_consteval_format_string() { return std::array {'{', '}', '\n'}; // "{}\n" } I can convert this array to std::string_view and invoke…
Elad Maimoni
  • 3,703
  • 3
  • 20
  • 37
1
vote
0 answers

c++ std::format from module crashes the program

I have a simple program in main.cpp file: import ; import ; int main() { std::cout << std::format("{}, {}!", "Hello", "world") << std::endl; } It uses iostream and format modules which are compiled with g++ -std=c++20…
Sun of A beach
  • 171
  • 1
  • 10
1
vote
1 answer

How could I extend a format-spec for existing type (eg float) in C++?

I want to extend formatting options for floating-point type, preferably preserving some of the existing options. For instance, I want to add a new type specifier for the measurements with units to write like this std::format({:.5mm}, 0.05f) and get…
Andrew
  • 311
  • 1
  • 7
1
vote
1 answer

std::format error : basic_format_string' is not a constant expression

Here is the code m_state.script_file( ( std::filesystem::path{ xorstr_( "magma/luas" ) } /= name ).string( ), [ & ] ( lua_State* const state, sol::protected_function_result result ) { if (…
Jerry Ho
  • 11
  • 1
1
vote
2 answers

How to forward formatting information from std::format

I am trying to use c++20's std::format on msvc. When I try to define a format for a custom type Person, I hope to be able to use the external std::format("{:x}{:*^10}", person) in "{:x}{:*^10}" is forwarded to std::formatter::fomat and used…
Ne C
  • 99
  • 6
0
votes
1 answer

Using std::format with custom types

I'm trying to compile an example from C++20 STL Cookbook. The example is about creating a specialization for std::formatter for a custom type, but I'm getting compilation error. I'm using GCC 13.1 on Windows. I'm getting the same issue on compiler…
Aamir
  • 1,974
  • 1
  • 14
  • 18
0
votes
1 answer

how to separate $n and numbers in std::regex_replace?

I have such a regex pattern: string text = "ekwfwierflwi..DATE..wdwedwd" const string pattern = "([\s\S]*)(DATE)([\s\S]*)"; Then I hope to replace date to a number sequence: const string format = fmt::format("$1{}$3", "2024-04.05…
barbyQAQ
  • 45
  • 5
0
votes
1 answer

How do I preserve inter-string null characters in std::format string

In an attempt to refactor this code: static const auto opf_str = [&]() { std::string result; for(auto& e : extension_list) { result.append(StringUtils::ToUpperCase(e.substr(1)) + " file (*"s + e + ")\0*"s + e + "\0"s); } …
Casey
  • 10,297
  • 11
  • 59
  • 88