fmt::basic_format_context里面保存了OutputIt,其实就是std::back_insert_iterator的一个子类,见
C++ STL insert_iterator插入迭代器适配器(深入了解,一文学会)-CSDN博客
用等于号(=),内部会调用push_back
#include <iostream>
#include <iterator>
#include <vector>
using namespace std;
//创建一个 vector 容器
vector<int> foo;
//创建一个可向 foo 容器尾部添加新元素的迭代器
back_insert_iterator< vector<int> > back_it(foo);
//将 5 插入到 foo 的末尾
back_it = 5;
//将 4 插入到当前 foo 的末尾
back_it = 4;
//将 3 插入到当前 foo 的末尾
back_it = 3;
//将 6 插入到当前 foo 的末尾
back_it = 6;
//输出 foo 容器中的元素
for (vector<int>::iterator it = foo.begin(); it != foo.end(); ++it)
cout << *it << ' ';
// Formatting context.
template <typename OutputIt, typename Char> class basic_format_context {
public:
/** The character type for the output. */
using char_type = Char;
private:
OutputIt out_;
basic_format_args<basic_format_context> args_;
detail::locale_ref loc_;
public:
using iterator = OutputIt;
using format_arg = basic_format_arg<basic_format_context>;
using parse_context_type = basic_format_parse_context<Char>;
template <typename T> using formatter_type = formatter<T, char_type>;
basic_format_context(basic_format_context&&) = default;
basic_format_context(const basic_format_context&) = delete;
void operator=(const basic_format_context&) = delete;
/**
Constructs a ``basic_format_context`` object. References to the arguments are
stored in the object so make sure they have appropriate lifetimes.
*/
basic_format_context(
OutputIt out, basic_format_args<basic_format_context> ctx_args,
detail::locale_ref loc = detail::locale_ref())
: out_(out), args_(ctx_args), loc_(loc) {}
auto arg(int id) const -> format_arg { return args_.get(id); }
auto arg(basic_string_view<char_type> name) -> format_arg {
return args_.get(name);
}
auto arg_id(basic_string_view<char_type> name) -> int {
return args_.get_id(name);
}
auto args() const -> const basic_format_args<basic_format_context>& {
return args_;
}
auto error_handler() -> detail::error_handler { return {}; }
void on_error(const char* message) { error_handler().on_error(message); }
// Returns an iterator to the beginning of the output range.
auto out() -> iterator { return out_; }
// Advances the begin iterator to ``it``.
void advance_to(iterator it) {
if (!detail::is_back_insert_iterator<iterator>()) out_ = it;
}
auto locale() -> detail::locale_ref { return loc_; }
};
会调用到advance_to这个函数
// Advances the begin iterator to ``it``.
void advance_to(iterator it) {
if (!detail::is_back_insert_iterator<iterator>()) out_ = it;
}
其中, out_ = it; operator = 内部调用的是push_back。