'std::format' Is Not a Member of 'std' Fix
Written November 28, 2026 · Tested with GCC 13.3.0 · 11 min read
- The error: the include is missing
- Neighboring headers: six fail, none rescues
- The fix
- Ordering, alignment, and bases
Written November 28, 2026
Tested with GCC 13.3.0
What this guide is
std::format is a function template, since C++20, defined in the header <format>. It formats its arguments according to a format string and returns the result as a string: replacement fields such as {} take the arguments in order, a field such as {:.2f} adds a format specification, and an argument index inside the braces chooses an argument out of order.
Because the function lives in one specific header, and only exists from C++20, 'format' is not a member of 'std' has two common causes:
- The
#include <format>line is missing in a C++20 build. For this error GCC prints no note at all: there is no line naming<format>as the header to add, unlike the note GCC prints for some neighboring names. The fix is still the include — the compiler just does not point at it here. - The build is not in C++20 mode at all. Under
-std=c++17the fixed program, with<format>included, fails with the identical single error and again no note, so this message alone cannot tell the two causes apart. That case has its own section below.
No neighboring header pulls this name in, not even by accident on this library, and one section below shows the sweep that settles that.
The error: the include is missing
This program formats two values into a string and prints it, but includes only <iostream> and <string>, compiled as C++20:
#include <iostream>
#include <string>
int main() {
std::string s = std::format("x={} y={:.2f}", 42, 3.14159);
std::cout << s << "\n";
return 0;
}
$ g++ -std=c++20 a_noinclude.cpp -o a_prog
a_noinclude.cpp: In function 'int main()':
a_noinclude.cpp:4:26: error: 'format' is not a member of 'std'
4 | std::string s = std::format("x={} y={:.2f}", 42, 3.14159);
| ^~~~~~
That single error is the whole output. There is no note under it naming a header, and no follow-on error: the compiler reports the unknown name once and stops there. Do not wait for a hint that never comes — a name in namespace std that the compiler does not know is, in the common case, a name whose header is not included, and the header for this one is <format>.
Neighboring headers: six fail, none rescues
Swapping in another nearby header changes nothing here. Compiled one at a time as the only extra header besides <iostream> and <string>, in C++20 mode, each of these fails with the same single error, 'format' is not a member of 'std':
<sstream><iomanip><string_view><utility><algorithm><ranges>
That list includes the stream formatting headers a program might already carry, <sstream> and <iomanip>, and a broad header, <ranges>, that does pull some other names in transitively on this library. None of them declares std::format on this libstdc++, not even transitively. There is no accidental rescue to stumble into here: a program that uses std::format includes <format>.
The fix
Add the header, and keep the build in C++20 mode or later:
#include <format>
#include <iostream>
#include <string>
int main() {
std::string s = std::format("x={} y={:.2f}", 42, 3.14159);
std::cout << s << "\n";
return 0;
}
$ g++ -std=c++20 a_fixed.cpp -o a_fixed_prog
$ ./a_fixed_prog
x=42 y=3.14
One include, <format>, declares the function. The empty field {} takes the first argument, 42, and the field {:.2f} takes the second argument with two digits after the decimal point, so 3.14159 prints as 3.14. The call returns the finished string, and the output statement prints it.
Ordering, alignment, and bases
The fixed shape does more than substitute values in order. An index inside the braces picks an argument by position, so {1} prints the second argument and {0} the first. A width pads the field, the <, >, and ^ signs align the value inside that width, and a letter after the colon chooses the base for an integer:
#include <format>
#include <iostream>
#include <string>
int main() {
std::cout << std::format("{1} {0}", "first", "second") << "\n";
std::cout << std::format("[{:>6}][{:<6}][{:^6}]", 42, 42, 42) << "\n";
std::cout << std::format("{:x} {:#x} {:b} {:o}", 255, 255, 255, 255) << "\n";
std::cout << std::format("{:.3f} {:+.2f} {:e}", 3.14159, 2.5, 12345.678) << "\n";
return 0;
}
$ g++ -std=c++20 b_rich.cpp -o b_prog
$ ./b_prog
second first
[ 42][42 ][ 42 ]
ff 0xff 11111111 377
3.142 +2.50 1.234568e+04
Read line by line: the positional fields swap the two words. The value 42 is right-aligned, left-aligned, then centered in fields six characters wide. The value 255 prints as ff in hexadecimal, 0xff with the # prefix form, 11111111 in binary, and 377 in octal. The last line rounds 3.14159 to three decimal places, forces a sign on 2.5, and writes 12345.678 in scientific notation.
Writing into a string or a buffer
The same header declares two relatives that write through an output iterator instead of returning a string. std::format_to writes the formatted result through the iterator it is given, and std::format_to_n writes no more than a stated number of characters and reports how many it would have written in the size member of its result:
#include <format>
#include <iostream>
#include <string>
int main() {
std::string out;
std::format_to(std::back_inserter(out), "{}-{}-{}", 2026, 11, 28);
std::cout << out << "\n";
std::cout << out.size() << "\n";
char buf[32];
auto res = std::format_to_n(buf, sizeof(buf) - 1, "x={}", 42);
*res.out = '\0';
std::cout << buf << "\n";
std::cout << res.size << "\n";
return 0;
}
$ g++ -std=c++20 c_format_to.cpp -o c_prog
$ ./c_prog
2026-11-28
10
x=42
4
The iterator form appends the date text to the string, which ends up 10 characters long. The bounded form writes x=42 into the array, the program terminates it by hand through the result's out member, and the reported size is 4, the full length of the text that was written.
Same error, same wording: the build is in C++17 mode
For most missing names, the note under the error separates a missing include from a build in the wrong language mode. Here it does not. Take the fixed program, with <format> included, and compile it as C++17 instead of C++20:
#include <format>
#include <iostream>
#include <string>
int main() {
std::string s = std::format("x={} y={:.2f}", 42, 3.14159);
std::cout << s << "\n";
return 0;
}
$ g++ -std=c++17 a_fixed.cpp -o x17_prog
a_fixed.cpp: In function 'int main()':
a_fixed.cpp:5:26: error: 'format' is not a member of 'std'
5 | std::string s = std::format("x={} y={:.2f}", 42, 3.14159);
| ^~~~~~
The include is present and the error is word-for-word the same single line, with no note in either build. The two causes are indistinguishable from the message: a missing include in a C++20 build and a present include in a C++17 build print exactly the same thing on this compiler. So check both halves every time — the #include <format> line in the file, and the -std flag on the build — because fixing only one of them leaves the other failure standing, with the same message waiting after the recompile.
A different wording: the name without its qualifier
Including <format> is not the only half of the name. This program includes the header but drops the std:: qualifier:
#include <format>
#include <iostream>
#include <string>
int main() {
std::string s = format("x={} y={:.2f}", 42, 3.14159);
std::cout << s << "\n";
return 0;
}
$ g++ -std=c++20 f_unqualified.cpp -o f_prog
f_unqualified.cpp: In function 'int main()':
f_unqualified.cpp:5:21: error: 'format' was not declared in this scope; did you mean 'std::format'?
5 | std::string s = format("x={} y={:.2f}", 42, 3.14159);
| ^~~~~~
| std::format
In file included from f_unqualified.cpp:1:
/usr/include/c++/13/format:3843:5: note: 'std::format' declared here
3843 | format(const locale& __loc, wformat_string<_Args...> __fmt,
| ^~~~~~
That wording — was not declared in this scope — is a different defect with a different fix: the header is present, the qualifier is not, and here the compiler does help, suggesting std::format and pointing at its declaration. Qualify the name as std::format; adding includes does not bring the name out of namespace std.
When the format string itself is wrong
A wrong format string is a different failure again, and where it surfaces depends on which function checks it. With a literal format string, std::format checks the string against the argument types at compile time, so a field with no matching argument never reaches run time:
#include <format>
#include <iostream>
#include <string>
int main() {
std::string s = std::format("{} {}", 1);
std::cout << s << "\n";
return 0;
}
$ g++ -std=c++20 e_runtime3.cpp -o e3_prog
In file included from e_runtime3.cpp:1:
/usr/include/c++/13/format: In function 'int main()':
e_runtime3.cpp:5:32: in 'constexpr' expansion of 'std::basic_format_string<char, int>("{} {}")'
/usr/include/c++/13/format:3751:19: in 'constexpr' expansion of '__scanner.std::__format::_Checking_scanner<char, int>::<anonymous>.std::__format::_Scanner<char>::_M_scan()'
/usr/include/c++/13/format:3559:30: in 'constexpr' expansion of '((std::__format::_Scanner<char>*)this)->std::__format::_Scanner<char>::_M_on_replacement_field()'
/usr/include/c++/13/format:3592:28: in 'constexpr' expansion of '((std::__format::_Scanner<char>*)this)->std::__format::_Scanner<char>::_M_pc.std::basic_format_parse_context<char>::next_arg_id()'
/usr/include/c++/13/format:232:56: error: call to non-'constexpr' function 'void std::__format::__invalid_arg_id_in_format_string()'
232 | __format::__invalid_arg_id_in_format_string();
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~
/usr/include/c++/13/format:182:3: note: 'void std::__format::__invalid_arg_id_in_format_string()' declared here
182 | __invalid_arg_id_in_format_string()
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The trace is long, but the reading is simple: the string asks for two arguments, one was given, and the compile-time check rejects the call. Supply the missing argument, or remove the extra field.
The non-template relative std::vformat takes its format string as an ordinary value, so no compile-time check applies to it, and the same kind of mistake surfaces at run time as a thrown std::format_error. Asking for an integer presentation from a text argument does this:
#include <format>
#include <iostream>
#include <string>
int main() {
try {
std::string s = std::vformat("{:d}", std::make_format_args("hello"));
std::cout << s << "\n";
} catch (const std::format_error& e) {
std::cout << "caught: " << e.what() << "\n";
}
return 0;
}
$ g++ -std=c++20 e_runtime.cpp -o e_prog
$ ./e_prog
caught: format error: failed to parse format-spec
And a field with no matching argument, passed the same way, throws with a different message:
#include <format>
#include <iostream>
#include <string>
int main() {
int one = 1;
try {
std::string s = std::vformat("{} {}", std::make_format_args(one));
std::cout << s << "\n";
} catch (const std::format_error& e) {
std::cout << "caught: " << e.what() << "\n";
}
return 0;
}
$ g++ -std=c++20 e_runtime2b.cpp -o e2b_prog
$ ./e2b_prog
caught: format error: invalid arg-id in format string
Those two what() texts — format error: failed to parse format-spec and format error: invalid arg-id in format string — are the messages this libstdc++ gives its std::format_error; another standard library words its exception text its own way. What carries across libraries is the shape: a run-time format string problem through std::vformat throws std::format_error, and catching it around the call turns a bad format string into a message instead of a terminated program.
The exact rule
std::formatis declared by#include <format>, and that is the only header required to declare it.<sstream>,<iomanip>,<string_view>,<utility>,<algorithm>, and<ranges>do not declare it, on this library not even transitively.- In a C++20 build,
'format' is not a member of 'std'in a program that includes only<iostream>and<string>beside the use means the<format>include is missing, and GCC gives no note naming the header for this error. The absence of a note is not evidence against the include being the fix. - In a C++17 build, the same single error, word-for-word, occurs with
<format>included, again with no note. The message cannot separate the two causes; the include and the language mode both have to be checked. std::format_to,std::format_to_n,std::vformat,std::make_format_args, andstd::format_errorare declared by the same header: the iterator-writing forms, the non-template formatting function, the argument-packing helper, and the exception type thrown on formatting errors.- With a literal format string,
std::formatperforms a compile-time check of the string against the arguments, and an invalid string is a compile error.std::vformattakes the string as a value and throwsstd::format_errorif the string is not valid for the provided arguments. 'format' was not declared in this scopefor an unqualifiedformatuse is a different defect: qualify the name asstd::format— the include alone does not bring the name out of namespacestd.- Providing more arguments than the format string requires is not an error; a field with no matching argument is.
The fixes, ranked
- First, check both halves, because the message does not separate them: is
#include <format>in the file that namesstd::format, and is the build in C++20 mode or later? Fix whichever half is wrong — or both. - Second, do not look for a note to confirm the include. For this name GCC prints none, in either build mode. The include is the fix in the common case whether or not the compiler points at it.
- Third, do not substitute a neighboring header.
<sstream>,<iomanip>,<string_view>,<utility>,<algorithm>, and<ranges>were each tried here and each fails; none of them pulls the name in by accident on this library. - Fourth, if the error says
was not declared in this scopeinstead, fix the name's qualifier tostd::formatas well. - Fifth, if the include and the mode are both right and the failure names the format string itself, read which stage caught it: a literal string fails at compile time, so fix the string or the argument list before recompiling; a string passed to
std::vformatthrows at run time, so catchstd::format_errorwhere the string comes from data the program does not control.
Which fix for which cause
#include <format>missing,std::formatused in a C++20 build with<iostream>and<string>present: the message is the single error'format' is not a member of 'std', with no note — add#include <format>.- Build in C++17 mode, with or without
#include <format>: the identical single message, again with no note — switch the build to C++20 mode or later, and keep the include. - Only
<sstream>,<iomanip>,<string_view>,<utility>,<algorithm>, or<ranges>included, in a C++20 build: same message, same missing name — add#include <format>. formatused without thestd::qualifier,<format>included: the message is'format' was not declared in this scope— qualify the name asstd::format.- A literal format string asks for more arguments than the call gives: the message is a compile error inside the header's checking machinery — supply the missing argument, or remove the extra field.
- A format string reaching
std::vformatis invalid for its arguments: the call throwsstd::format_errorat run time — fix the string or the arguments, and catch the exception where the string is not known at compile time.
Checklist
- Check the include list of the file that names
std::format, not just the project. The include must be in that file, or in a header that file includes directly by design. - Confirm the build is in C++20 mode or later.
std::formatdoes not exist in earlier modes, with or without its header, and the error message looks the same either way. - Do not read the missing note as a clue. Some neighboring names get a header note from this compiler and this one does not; the include check and the mode check are both manual here.
- Do not rely on a neighboring header pulling
std::formatin. All six headers tried here fail, including<ranges>. - One header per name the file uses: the result string comes from
<string>and output comes from<iostream>, even in a program whose subject is the formatting call. - After the fix, confirm the program runs, not just that it compiles: the fixed program here prints
x=42 y=3.14. - Where a format string arrives at run time, route it through
std::vformatand catchstd::format_error; where it is a literal in the source, let the compile-time check catch mistakes before the program ships.
Related guides
- 'std::print' Is Not a Member of 'std' Fix — the same missing-include shape for the printing functions in
<print>, the other half of the text formatting library. - 'std::array' Is Not a Member of 'std' Fix — the same missing-include shape for the fixed-size container in
<array>. - 'std::optional' Is Not a Member of 'std' Fix — the same missing-include shape for the vocabulary type in
<optional>.
Sources
- cppreference,
std::format— forstd::format(since C++20) being defined in header<format>, formatting its arguments according to the format string and returning the result as a string, for replacement fields with an optional argument index and format specification, for the compile-time check on the format string and the direction to usestd::vformatwhen the string is not a compile-time constant, and for providing more arguments than the string requires not being an error. - cppreference, Standard library header
<format>— for<format>(C++20), part of the text processing library, declaringformat,format_to,format_to_n,vformat, andmake_format_args(all C++20), thebasic_format_stringclass template that performs compile-time format string checks at construction time, andformat_error(C++20) as the exception type thrown on formatting errors. - cppreference,
std::vformat— forstd::vformat(since C++20) being defined in header<format>, formatting the arguments held by itsstd::format_argsargument according to the format string, and throwingstd::format_errorif the format string is not valid for the provided arguments.
Keep Exploring
Seven tips and seven puzzles so far, each one a short runnable example with the fix explained.
Paste an AddressSanitizer, UBSan, or ThreadSanitizer report and get the bug, the file and line, and the fix \u2014 in plain language.
C++20 and C++23 features, migration paths, and measured trade-offs.
The full C++ hub \u2014 language basics, the standard library, coroutines, and measured comparisons.
Paste a [Service] section and get a plain-language verdict on every sandboxing setting it finds.