From: Mikko Rasa Date: Tue, 2 Aug 2016 09:16:25 +0000 (+0300) Subject: Use variadic templates for format and print functions if available X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=commitdiff_plain;h=4e8408b966dfe65d971237c72a892f4720d885b1 Use variadic templates for format and print functions if available --- diff --git a/source/io/print.h b/source/io/print.h index 8e9fde5..29da9f4 100644 --- a/source/io/print.h +++ b/source/io/print.h @@ -15,6 +15,12 @@ completeness with the other print functions. inline unsigned print(Base &o, const std::string &f) { return o.write(f); } +#if __cplusplus>=201103L +template +inline unsigned print(Base &o, const std::string &f, Args... args) +{ return print(o, format(f, args...)); } + +#else template inline unsigned print(Base &o, const std::string &f, A1 a1) { return print(o, format(f, a1)); } @@ -34,12 +40,19 @@ inline unsigned print(Base &o, const std::string &f, A1 a1, A2 a2, A3 a3, A4 a4) template inline unsigned print(Base &o, const std::string &f, A1 a1, A2 a2, A3 a3, A4 a4, A5 a5) { return print(o, format(f, a1, a2, a3, a4, a5)); } +#endif /* The following functions print to console */ inline unsigned print(const std::string &f) { return print(cout, f); } +#if __cplusplus>=201103L +template +inline unsigned print(const std::string &f, Args... args) +{ return print(cout, f, args...); } + +#else template inline unsigned print(const std::string &f, A1 a1) { return print(cout, f, a1); } @@ -59,6 +72,7 @@ inline unsigned print(const std::string &f, A1 a1, A2 a2, A3 a3, A4 a4) template inline unsigned print(const std::string &f, A1 a1, A2 a2, A3 a3, A4 a4, A5 a5) { return print(cout, f, a1, a2, a3, a4, a5); } +#endif } // namespace IO } // namespace Msp diff --git a/source/strings/format.h b/source/strings/format.h index 65d10df..7bd8264 100644 --- a/source/strings/format.h +++ b/source/strings/format.h @@ -29,6 +29,14 @@ public: return *this; } +#if __cplusplus>=201103L + template + Formatter &operator()(const T &a, const Tail &... tail) + { + return (*this)(a)(tail...); + } +#endif + /** Returns the result of the formatting operation. Will throw if not enough values have been fed to the formatter. */ const std::string &str() const; @@ -47,6 +55,14 @@ private: inline Formatter format(const std::string &f) { return Formatter(f); } +#if __cplusplus >= 201103L +template +inline std::string format(const std::string &f, const Args &... args) +{ + return Formatter(f)(args...).str(); +} + +#else template inline std::string format(const std::string &f, const A1 &a1) { return Formatter(f)(a1).str(); } @@ -66,6 +82,7 @@ inline std::string format(const std::string &f, const A1 &a1, const A2 &a2, cons template inline std::string format(const std::string &f, const A1 &a1, const A2 &a2, const A3 &a3, const A4 &a4, const A5 &a5) { return Formatter(f)(a1)(a2)(a3)(a4)(a5).str(); } +#endif } // namespace Msp