From 7f6089b5f63c936715e042515269842c56c7de04 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Tue, 4 Sep 2018 22:19:30 +0100 Subject: scprintf macro with non-variable Updates scprintf macro to handle the case where strm isn't a variable and thus cannot be captured, instead passing it as a parameter to the lambda. --- libadhocutil/compileTimeFormatter.h | 8 ++++---- libadhocutil/unittests/testCompileTimeFormatter.cpp | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/libadhocutil/compileTimeFormatter.h b/libadhocutil/compileTimeFormatter.h index 2fc193e..35dffde 100644 --- a/libadhocutil/compileTimeFormatter.h +++ b/libadhocutil/compileTimeFormatter.h @@ -190,11 +190,11 @@ namespace AdHoc { // As far as I know, only clang/llvm version 5+ can compile this // so long as std=c++17 #if __clang_major__ >= 5 && __cplusplus >= 201703 -#define scprintf(strm, fmt, ...) \ - ([&strm]() -> decltype(strm) & { \ - static constexpr const char * const __FMT = fmt; \ +#define scprintf(strmp, fmt, ...) \ + ([](decltype(strmp) & strm) -> auto & { \ + static constexpr auto __FMT = fmt; \ return ::AdHoc::Formatter<__FMT>::write(strm, __VA_ARGS__); \ - }()) + }(strmp)) #endif #endif diff --git a/libadhocutil/unittests/testCompileTimeFormatter.cpp b/libadhocutil/unittests/testCompileTimeFormatter.cpp index b966078..9bb81f6 100644 --- a/libadhocutil/unittests/testCompileTimeFormatter.cpp +++ b/libadhocutil/unittests/testCompileTimeFormatter.cpp @@ -450,10 +450,10 @@ BOOST_AUTO_TEST_CASE(smartptr) #ifdef scprintf BOOST_AUTO_TEST_CASE(scprintf) { - std::stringstream str; - auto & strret = scprintf(str, "Some literal format string (%d, %c).", 0, 'f'); - BOOST_CHECK_EQUAL(&str, &strret); // We got back our original stream - BOOST_CHECK_EQUAL(str.str(), "Some literal format string (0, f)."); + auto str = std::make_unique(); + auto & strret = scprintf(*str, "Some literal format string (%d, %c).", 0, 'f'); + BOOST_CHECK_EQUAL(str.get(), &strret); // We got back our original stream + BOOST_CHECK_EQUAL(str->str(), "Some literal format string (0, f)."); } #endif -- cgit v1.2.3