I am getting a stack-use-after-scope error when trying to use std::format_args along with std::vformat(). A simple code to reproduce it is:
#include <format>
#include <iostream>
#include <string>
int main() {
int i = 42;
std::string s = "hello";
std::format_args args = std::make_format_args(i, s);
std::cout << std::vformat("Number: {}, Word: {}", args) << '\n';
return 0;
}
My i and s variables should still be in scope and are not r-values, so I'm not sure what is triggering the error. If I remove args and make a call to std::make_format_args() directly in the call to std::vformat(), everything works as expected. What am I missing?
Here is my command to compile the above code: clang++-17 --std=c++23 -stdlib=libc++ -fsanitize=address fmt.cpp.
std::make_format_args(i, s);does not returnstd::format_args args. The result of the unspecified type can be implicitly converted tostd::format_args argsthat keeps a dangling reference to some internal state in the returned temporary object.The proper usage is:
https://godbolt.org/z/Tozs7a9bP
The better usage is: