From 7c50da5385cb9e67e9be37544caa1649fd466dae Mon Sep 17 00:00:00 2001 From: Hannes Harnisch Date: Thu, 19 Dec 2024 04:54:35 +0100 Subject: [PATCH] Allow getting size of dynamic format arg store (#4270) --- include/fmt/args.h | 3 +++ test/args-test.cc | 14 ++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/include/fmt/args.h b/include/fmt/args.h index 6ed30c0bd5c0..3ff47880748f 100644 --- a/include/fmt/args.h +++ b/include/fmt/args.h @@ -210,6 +210,9 @@ template class dynamic_format_arg_store { data_.reserve(new_cap); named_info_.reserve(new_cap_named); } + + /// Returns the number of elements in the store. + size_t size() const noexcept { return data_.size(); } }; FMT_END_NAMESPACE diff --git a/test/args-test.cc b/test/args-test.cc index f99ff74ed192..e3efa244bfb7 100644 --- a/test/args-test.cc +++ b/test/args-test.cc @@ -186,3 +186,17 @@ TEST(args_test, move_constructor) { store.reset(); EXPECT_EQ(fmt::vformat("{} {} {a1}", moved_store), "42 foo foo"); } + +TEST(args_test, size) { + fmt::dynamic_format_arg_store store; + EXPECT_EQ(store.size(), 0); + + store.push_back(42); + EXPECT_EQ(store.size(), 1); + + store.push_back("Molybdenum"); + EXPECT_EQ(store.size(), 2); + + store.clear(); + EXPECT_EQ(store.size(), 0); +}