From 2ba1573ac37301d50fa2388ce3087e0f09005487 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Tue, 18 Dec 2012 14:38:03 -0800 Subject: [PATCH] Introduce str and c_str into the format namespace. --- format.h | 16 ++++++++++------ format_test.cc | 7 ++++++- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/format.h b/format.h index eac6744c..23c1ba82 100644 --- a/format.h +++ b/format.h @@ -380,20 +380,24 @@ class ArgInserter { return Proxy(f); } - std::string str() const { return Format()->str(); } + // Performs formatting and returns a std::string with the output. + friend std::string str(Proxy p) { + return Format(p)->str(); + } // Performs formatting and returns a C string with the output. friend const char *c_str(Proxy p) { return Format(p)->c_str(); } - - // Performs formatting and returns a std::string with the output. - friend std::string str(Proxy p) { - return Format(p)->str(); - } }; + +const char *c_str(ArgInserter::Proxy p); +std::string str(ArgInserter::Proxy p); } +using format::internal::c_str; +using format::internal::str; + // ArgFormatter provides access to the format buffer within custom // Format functions. It is not desirable to pass Formatter to these // functions because Formatter::operator() is not reentrant and diff --git a/format_test.cc b/format_test.cc index a3d489cc..3f518cff 100644 --- a/format_test.cc +++ b/format_test.cc @@ -729,6 +729,11 @@ TEST(FormatterTest, ArgInserter) { EXPECT_STREQ("12", c_str(format("{0}") << 2)); } +TEST(FormatterTest, StrNamespace) { + fmt::str(Format("")); + fmt::c_str(Format("")); +} + struct CountCalls { int &num_calls; @@ -791,7 +796,7 @@ TEST(TempFormatterTest, Example) { template std::string str(const T &value) { - return (fmt::Format("{0}") << value).str(); + return fmt::str(fmt::Format("{0}") << value); } TEST(StrTest, Convert) {