lmi-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[lmi-commits] [lmi] master 3861b87 1/4: Demonstrate a syntactic alternat


From: Greg Chicares
Subject: [lmi-commits] [lmi] master 3861b87 1/4: Demonstrate a syntactic alternative
Date: Fri, 18 Jan 2019 08:50:50 -0500 (EST)

branch: master
commit 3861b870ff0aaf384afaaf5670250cf0f5fd8c51
Author: Gregory W. Chicares <address@hidden>
Commit: Gregory W. Chicares <address@hidden>

    Demonstrate a syntactic alternative
---
 stream_cast_test.cpp | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)

diff --git a/stream_cast_test.cpp b/stream_cast_test.cpp
index 21473db..0636eda 100644
--- a/stream_cast_test.cpp
+++ b/stream_cast_test.cpp
@@ -95,6 +95,31 @@ To cast_3(From from, To = To())
     return result;
 }
 
+/// Like cast_3<>(), but with a lambda instead of an auxiliary function.
+
+template<typename To, typename From>
+To cast_4(From from, To = To())
+{
+    static std::stringstream interpreter = []
+        {
+        std::stringstream ss {};
+        ss.imbue(blank_is_not_whitespace_locale());
+        return ss;
+        } ();
+    interpreter.str(std::string{});
+    interpreter.clear();
+    To result = To();
+    if
+        (  !(interpreter << from)
+        || !(interpreter >> result)
+        || !(interpreter >> std::ws).eof()
+        )
+        {
+        throw std::runtime_error("Oops!");
+        }
+    return result;
+}
+
 void assay_speed()
 {
     static double const e {2.718281828459045};
@@ -102,12 +127,14 @@ void assay_speed()
     auto f1 = [] {for(int n = 0; n < 1000; ++n) cast_1     <std::string>(e);};
     auto f2 = [] {for(int n = 0; n < 1000; ++n) cast_2     <std::string>(e);};
     auto f3 = [] {for(int n = 0; n < 1000; ++n) cast_3     <std::string>(e);};
+    auto f4 = [] {for(int n = 0; n < 1000; ++n) cast_4     <std::string>(e);};
     std::cout
         << "\n  Speed tests..."
         << "\n  stream_cast     : " << TimeAnAliquot(f0)
         << "\n  minimalistic    : " << TimeAnAliquot(f1)
         << "\n  static stream   : " << TimeAnAliquot(f2)
         << "\n  static facet too: " << TimeAnAliquot(f3)
+        << "\n  same, but IIFE  : " << TimeAnAliquot(f4)
         << std::endl
         ;
 }



reply via email to

[Prev in Thread] Current Thread [Next in Thread]