Topic: Create std::lazy_t that implements lazy evaluation


Author: Antonio Perez <antonio@perezexcelsior.com>
Date: Thu, 24 May 2018 12:01:50 -0700 (PDT)
Raw View
------=_Part_8599_608758115.1527188510195
Content-Type: multipart/alternative;
 boundary="----=_Part_8600_1161028995.1527188510196"

------=_Part_8600_1161028995.1527188510196
Content-Type: text/plain; charset="UTF-8"

Lazy evaluation is a powerful abstraction with a plethora of uses. While it
traditionally appears in fully-functional languages, due to C++'s
flexibility it's possible to implement lazy evaluation within C++ in a way
that leverages the strengths of the language.

Below is an example implementation done in C++17. Here lazy_t stores a
callable object, which it invokes and returns when the implicit conversion
operator is invoked.

template<class func_t>
struct lazy_t {
   typedef decltype(std::declval<func_t>()()) value_type;
   func_t func;
   lazy_t() = default;
   lazy_t(const func_t& func) : func(func) {}
   lazy_t(func_t&& func) : func(std::move(func)) {}
   operator value_type() {
       return func();
   }
   operator value_type() const {
       return func();
   }
   template<class assignment_t>
   lazy_t& operator=(assignment_t&& value) {
       func(std::forward<decltype(value)>(value));
       return *this;
   }
};
template<class func_t>
struct lazy_t<func_t&> {
   typedef decltype(std::declval<func_t>()()) value_type;
   func_t& func;
   lazy_t(func_t& func) : func(func) {}
   operator value_type() {
       return func();
   }
   operator value_type() const {
       return func();
   }
   template<class assignment_t>
   lazy_t& operator=(assignment_t&& value) {
       func(std::forward<decltype(value)>(value));
       return *this;
   }
};

A programmer could pass the lazy type to a function which might require an
object that would otherwise be expensive to construct: if the function
doesn't require the object, it's never constructed, and if the function
does require it, the lazy type constructs and returns the object.
Internally, the callable object stored in the lazy type could store a
std::optional.

If class template argument deduction is allowed within the body of a class,
lazy types could also be used to implement properties. A lazy_type could
also be used during debugging or benchmarking to count the number of
accesses or assignments to a variable, and lazy_types stored in a vector
would be a convenient way of solving problems where indirection is
required.

--
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/e2995c19-feec-49d5-be8b-aca9ff08ac2e%40isocpp.org.

------=_Part_8600_1161028995.1527188510196
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">Lazy evaluation is a powerful abstraction with a plethora =
of uses. While it traditionally appears in fully-functional languages, due =
to C++&#39;s flexibility it&#39;s possible to implement lazy evaluation wit=
hin C++ in a way that leverages the strengths of the language.=C2=A0<div><b=
r></div><div>Below is an example implementation done in C++17. Here lazy_t =
stores a callable object, which it invokes and returns when the implicit co=
nversion operator is invoked.<div><div>=C2=A0<div class=3D"prettyprint" sty=
le=3D"border: 1px solid rgb(187, 187, 187); word-wrap: break-word;"><code c=
lass=3D"prettyprint"><div class=3D"subprettyprint"><div style=3D"font-famil=
y: Menlo, Monaco, &quot;Courier New&quot;, monospace; font-size: 12px; line=
-height: 18px; white-space: pre;"><font color=3D"#ffffff" style=3D"backgrou=
nd-color: rgb(255, 255, 255);"><div><span style=3D"color: #008;" class=3D"s=
tyled-by-prettify">template</span><span style=3D"color: #660;" class=3D"sty=
led-by-prettify">&lt;</span><span style=3D"color: #008;" class=3D"styled-by=
-prettify">class</span><span style=3D"color: #000;" class=3D"styled-by-pret=
tify"> func_t</span><span style=3D"color: #660;" class=3D"styled-by-prettif=
y">&gt;</span></div><div><span style=3D"color: #008;" class=3D"styled-by-pr=
ettify">struct</span><span style=3D"color: #000;" class=3D"styled-by-pretti=
fy"> lazy_t </span><span style=3D"color: #660;" class=3D"styled-by-prettify=
">{</span></div><div><span style=3D"color: #000;" class=3D"styled-by-pretti=
fy"> =C2=A0 =C2=A0</span><span style=3D"color: #008;" class=3D"styled-by-pr=
ettify">typedef</span><span style=3D"color: #000;" class=3D"styled-by-prett=
ify"> </span><span style=3D"color: #008;" class=3D"styled-by-prettify">decl=
type</span><span style=3D"color: #660;" class=3D"styled-by-prettify">(</spa=
n><span style=3D"color: #000;" class=3D"styled-by-prettify">std</span><span=
 style=3D"color: #660;" class=3D"styled-by-prettify">::</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify">declval</span><span style=3D=
"color: #080;" class=3D"styled-by-prettify">&lt;func_t&gt;</span><span styl=
e=3D"color: #660;" class=3D"styled-by-prettify">()())</span><span style=3D"=
color: #000;" class=3D"styled-by-prettify"> value_type</span><span style=3D=
"color: #660;" class=3D"styled-by-prettify">;</span></div><div><span style=
=3D"color: #000;" class=3D"styled-by-prettify"> =C2=A0 =C2=A0func_t func</s=
pan><span style=3D"color: #660;" class=3D"styled-by-prettify">;</span></div=
><div><span style=3D"color: #000;" class=3D"styled-by-prettify"> =C2=A0 =C2=
=A0lazy_t</span><span style=3D"color: #660;" class=3D"styled-by-prettify">(=
)</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><=
span style=3D"color: #660;" class=3D"styled-by-prettify">=3D</span><span st=
yle=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"co=
lor: #008;" class=3D"styled-by-prettify">default</span><span style=3D"color=
: #660;" class=3D"styled-by-prettify">;</span></div><div><span style=3D"col=
or: #000;" class=3D"styled-by-prettify"> =C2=A0 =C2=A0lazy_t</span><span st=
yle=3D"color: #660;" class=3D"styled-by-prettify">(</span><span style=3D"co=
lor: #008;" class=3D"styled-by-prettify">const</span><span style=3D"color: =
#000;" class=3D"styled-by-prettify"> func_t</span><span style=3D"color: #66=
0;" class=3D"styled-by-prettify">&amp;</span><span style=3D"color: #000;" c=
lass=3D"styled-by-prettify"> func</span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">)</span><span style=3D"color: #000;" class=3D"style=
d-by-prettify"> </span><span style=3D"color: #660;" class=3D"styled-by-pret=
tify">:</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> fu=
nc</span><span style=3D"color: #660;" class=3D"styled-by-prettify">(</span>=
<span style=3D"color: #000;" class=3D"styled-by-prettify">func</span><span =
style=3D"color: #660;" class=3D"styled-by-prettify">)</span><span style=3D"=
color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color: #6=
60;" class=3D"styled-by-prettify">{}</span></div><div><span style=3D"color:=
 #000;" class=3D"styled-by-prettify"> =C2=A0 =C2=A0lazy_t</span><span style=
=3D"color: #660;" class=3D"styled-by-prettify">(</span><span style=3D"color=
: #000;" class=3D"styled-by-prettify">func_t</span><span style=3D"color: #6=
60;" class=3D"styled-by-prettify">&amp;&amp;</span><span style=3D"color: #0=
00;" class=3D"styled-by-prettify"> func</span><span style=3D"color: #660;" =
class=3D"styled-by-prettify">)</span><span style=3D"color: #000;" class=3D"=
styled-by-prettify"> </span><span style=3D"color: #660;" class=3D"styled-by=
-prettify">:</span><span style=3D"color: #000;" class=3D"styled-by-prettify=
"> func</span><span style=3D"color: #660;" class=3D"styled-by-prettify">(</=
span><span style=3D"color: #000;" class=3D"styled-by-prettify">std</span><s=
pan style=3D"color: #660;" class=3D"styled-by-prettify">::</span><span styl=
e=3D"color: #000;" class=3D"styled-by-prettify">move</span><span style=3D"c=
olor: #660;" class=3D"styled-by-prettify">(</span><span style=3D"color: #00=
0;" class=3D"styled-by-prettify">func</span><span style=3D"color: #660;" cl=
ass=3D"styled-by-prettify">))</span><span style=3D"color: #000;" class=3D"s=
tyled-by-prettify"> </span><span style=3D"color: #660;" class=3D"styled-by-=
prettify">{}</span></div><div><span style=3D"color: #000;" class=3D"styled-=
by-prettify"> =C2=A0 =C2=A0</span><span style=3D"color: #008;" class=3D"sty=
led-by-prettify">operator</span><span style=3D"color: #000;" class=3D"style=
d-by-prettify"> value_type</span><span style=3D"color: #660;" class=3D"styl=
ed-by-prettify">()</span><span style=3D"color: #000;" class=3D"styled-by-pr=
ettify"> </span><span style=3D"color: #660;" class=3D"styled-by-prettify">{=
</span></div><div><span style=3D"color: #000;" class=3D"styled-by-prettify"=
> =C2=A0 =C2=A0 =C2=A0 =C2=A0</span><span style=3D"color: #008;" class=3D"s=
tyled-by-prettify">return</span><span style=3D"color: #000;" class=3D"style=
d-by-prettify"> func</span><span style=3D"color: #660;" class=3D"styled-by-=
prettify">();</span></div><div><span style=3D"color: #000;" class=3D"styled=
-by-prettify"> =C2=A0 =C2=A0</span><span style=3D"color: #660;" class=3D"st=
yled-by-prettify">}</span></div><div><span style=3D"color: #000;" class=3D"=
styled-by-prettify"> =C2=A0 =C2=A0</span><span style=3D"color: #008;" class=
=3D"styled-by-prettify">operator</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify"> value_type</span><span style=3D"color: #660;" clas=
s=3D"styled-by-prettify">()</span><span style=3D"color: #000;" class=3D"sty=
led-by-prettify"> </span><span style=3D"color: #008;" class=3D"styled-by-pr=
ettify">const</span><span style=3D"color: #000;" class=3D"styled-by-prettif=
y"> </span><span style=3D"color: #660;" class=3D"styled-by-prettify">{</spa=
n></div><div><span style=3D"color: #000;" class=3D"styled-by-prettify"> =C2=
=A0 =C2=A0 =C2=A0 =C2=A0</span><span style=3D"color: #008;" class=3D"styled=
-by-prettify">return</span><span style=3D"color: #000;" class=3D"styled-by-=
prettify"> func</span><span style=3D"color: #660;" class=3D"styled-by-prett=
ify">();</span></div><div><span style=3D"color: #000;" class=3D"styled-by-p=
rettify"> =C2=A0 =C2=A0</span><span style=3D"color: #660;" class=3D"styled-=
by-prettify">}</span></div><div><span style=3D"color: #000;" class=3D"style=
d-by-prettify"> =C2=A0 =C2=A0</span><span style=3D"color: #008;" class=3D"s=
tyled-by-prettify">template</span><span style=3D"color: #660;" class=3D"sty=
led-by-prettify">&lt;</span><span style=3D"color: #008;" class=3D"styled-by=
-prettify">class</span><span style=3D"color: #000;" class=3D"styled-by-pret=
tify"> assignment_t</span><span style=3D"color: #660;" class=3D"styled-by-p=
rettify">&gt;</span></div><div><span style=3D"color: #000;" class=3D"styled=
-by-prettify"> =C2=A0 =C2=A0lazy_t</span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">&amp;</span><span style=3D"color: #000;" class=3D"s=
tyled-by-prettify"> </span><span style=3D"color: #008;" class=3D"styled-by-=
prettify">operator</span><span style=3D"color: #660;" class=3D"styled-by-pr=
ettify">=3D(</span><span style=3D"color: #000;" class=3D"styled-by-prettify=
">assignment_t</span><span style=3D"color: #660;" class=3D"styled-by-pretti=
fy">&amp;&amp;</span><span style=3D"color: #000;" class=3D"styled-by-pretti=
fy"> value</span><span style=3D"color: #660;" class=3D"styled-by-prettify">=
)</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><=
span style=3D"color: #660;" class=3D"styled-by-prettify">{</span></div><div=
><span style=3D"color: #000;" class=3D"styled-by-prettify"> =C2=A0 =C2=A0 =
=C2=A0 =C2=A0func</span><span style=3D"color: #660;" class=3D"styled-by-pre=
ttify">(</span><span style=3D"color: #000;" class=3D"styled-by-prettify">st=
d</span><span style=3D"color: #660;" class=3D"styled-by-prettify">::</span>=
<span style=3D"color: #000;" class=3D"styled-by-prettify">forward</span><sp=
an style=3D"color: #660;" class=3D"styled-by-prettify">&lt;</span><span sty=
le=3D"color: #008;" class=3D"styled-by-prettify">decltype</span><span style=
=3D"color: #660;" class=3D"styled-by-prettify">(</span><span style=3D"color=
: #000;" class=3D"styled-by-prettify">value</span><span style=3D"color: #66=
0;" class=3D"styled-by-prettify">)&gt;(</span><span style=3D"color: #000;" =
class=3D"styled-by-prettify">value</span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">));</span></div><div><span style=3D"color: #000;" c=
lass=3D"styled-by-prettify"> =C2=A0 =C2=A0 =C2=A0 =C2=A0</span><span style=
=3D"color: #008;" class=3D"styled-by-prettify">return</span><span style=3D"=
color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color: #6=
60;" class=3D"styled-by-prettify">*</span><span style=3D"color: #008;" clas=
s=3D"styled-by-prettify">this</span><span style=3D"color: #660;" class=3D"s=
tyled-by-prettify">;</span></div><div><span style=3D"color: #000;" class=3D=
"styled-by-prettify"> =C2=A0 =C2=A0</span><span style=3D"color: #660;" clas=
s=3D"styled-by-prettify">}</span></div><div><span style=3D"color: #660;" cl=
ass=3D"styled-by-prettify">};</span></div><div><span style=3D"color: #008;"=
 class=3D"styled-by-prettify">template</span><span style=3D"color: #660;" c=
lass=3D"styled-by-prettify">&lt;</span><span style=3D"color: #008;" class=
=3D"styled-by-prettify">class</span><span style=3D"color: #000;" class=3D"s=
tyled-by-prettify"> func_t</span><span style=3D"color: #660;" class=3D"styl=
ed-by-prettify">&gt;</span></div><div><span style=3D"color: #008;" class=3D=
"styled-by-prettify">struct</span><span style=3D"color: #000;" class=3D"sty=
led-by-prettify"> lazy_t</span><span style=3D"color: #660;" class=3D"styled=
-by-prettify">&lt;</span><span style=3D"color: #000;" class=3D"styled-by-pr=
ettify">func_t</span><span style=3D"color: #660;" class=3D"styled-by-pretti=
fy">&amp;&gt;</span><span style=3D"color: #000;" class=3D"styled-by-prettif=
y"> </span><span style=3D"color: #660;" class=3D"styled-by-prettify">{</spa=
n></div><div><span style=3D"color: #000;" class=3D"styled-by-prettify"> =C2=
=A0 =C2=A0</span><span style=3D"color: #008;" class=3D"styled-by-prettify">=
typedef</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </=
span><span style=3D"color: #008;" class=3D"styled-by-prettify">decltype</sp=
an><span style=3D"color: #660;" class=3D"styled-by-prettify">(</span><span =
style=3D"color: #000;" class=3D"styled-by-prettify">std</span><span style=
=3D"color: #660;" class=3D"styled-by-prettify">::</span><span style=3D"colo=
r: #000;" class=3D"styled-by-prettify">declval</span><span style=3D"color: =
#080;" class=3D"styled-by-prettify">&lt;func_t&gt;</span><span style=3D"col=
or: #660;" class=3D"styled-by-prettify">()())</span><span style=3D"color: #=
000;" class=3D"styled-by-prettify"> value_type</span><span style=3D"color: =
#660;" class=3D"styled-by-prettify">;</span></div><div><span style=3D"color=
: #000;" class=3D"styled-by-prettify"> =C2=A0 =C2=A0func_t</span><span styl=
e=3D"color: #660;" class=3D"styled-by-prettify">&amp;</span><span style=3D"=
color: #000;" class=3D"styled-by-prettify"> func</span><span style=3D"color=
: #660;" class=3D"styled-by-prettify">;</span></div><div><span style=3D"col=
or: #000;" class=3D"styled-by-prettify"> =C2=A0 =C2=A0lazy_t</span><span st=
yle=3D"color: #660;" class=3D"styled-by-prettify">(</span><span style=3D"co=
lor: #000;" class=3D"styled-by-prettify">func_t</span><span style=3D"color:=
 #660;" class=3D"styled-by-prettify">&amp;</span><span style=3D"color: #000=
;" class=3D"styled-by-prettify"> func</span><span style=3D"color: #660;" cl=
ass=3D"styled-by-prettify">)</span><span style=3D"color: #000;" class=3D"st=
yled-by-prettify"> </span><span style=3D"color: #660;" class=3D"styled-by-p=
rettify">:</span><span style=3D"color: #000;" class=3D"styled-by-prettify">=
 func</span><span style=3D"color: #660;" class=3D"styled-by-prettify">(</sp=
an><span style=3D"color: #000;" class=3D"styled-by-prettify">func</span><sp=
an style=3D"color: #660;" class=3D"styled-by-prettify">)</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color=
: #660;" class=3D"styled-by-prettify">{}</span></div><div><span style=3D"co=
lor: #000;" class=3D"styled-by-prettify"> =C2=A0 =C2=A0</span><span style=
=3D"color: #008;" class=3D"styled-by-prettify">operator</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify"> value_type</span><span styl=
e=3D"color: #660;" class=3D"styled-by-prettify">()</span><span style=3D"col=
or: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color: #660;=
" class=3D"styled-by-prettify">{</span></div><div><span style=3D"color: #00=
0;" class=3D"styled-by-prettify"> =C2=A0 =C2=A0 =C2=A0 =C2=A0</span><span s=
tyle=3D"color: #008;" class=3D"styled-by-prettify">return</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify"> func</span><span style=3D"c=
olor: #660;" class=3D"styled-by-prettify">();</span></div><div><span style=
=3D"color: #000;" class=3D"styled-by-prettify"> =C2=A0 =C2=A0</span><span s=
tyle=3D"color: #660;" class=3D"styled-by-prettify">}</span></div><div><span=
 style=3D"color: #000;" class=3D"styled-by-prettify"> =C2=A0 =C2=A0</span><=
span style=3D"color: #008;" class=3D"styled-by-prettify">operator</span><sp=
an style=3D"color: #000;" class=3D"styled-by-prettify"> value_type</span><s=
pan style=3D"color: #660;" class=3D"styled-by-prettify">()</span><span styl=
e=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"colo=
r: #008;" class=3D"styled-by-prettify">const</span><span style=3D"color: #0=
00;" class=3D"styled-by-prettify"> </span><span style=3D"color: #660;" clas=
s=3D"styled-by-prettify">{</span></div><div><span style=3D"color: #000;" cl=
ass=3D"styled-by-prettify"> =C2=A0 =C2=A0 =C2=A0 =C2=A0</span><span style=
=3D"color: #008;" class=3D"styled-by-prettify">return</span><span style=3D"=
color: #000;" class=3D"styled-by-prettify"> func</span><span style=3D"color=
: #660;" class=3D"styled-by-prettify">();</span></div><div><span style=3D"c=
olor: #000;" class=3D"styled-by-prettify"> =C2=A0 =C2=A0</span><span style=
=3D"color: #660;" class=3D"styled-by-prettify">}</span></div><div><span sty=
le=3D"color: #000;" class=3D"styled-by-prettify"> =C2=A0 =C2=A0</span><span=
 style=3D"color: #008;" class=3D"styled-by-prettify">template</span><span s=
tyle=3D"color: #660;" class=3D"styled-by-prettify">&lt;</span><span style=
=3D"color: #008;" class=3D"styled-by-prettify">class</span><span style=3D"c=
olor: #000;" class=3D"styled-by-prettify"> assignment_t</span><span style=
=3D"color: #660;" class=3D"styled-by-prettify">&gt;</span></div><div><span =
style=3D"color: #000;" class=3D"styled-by-prettify"> =C2=A0 =C2=A0lazy_t</s=
pan><span style=3D"color: #660;" class=3D"styled-by-prettify">&amp;</span><=
span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span styl=
e=3D"color: #008;" class=3D"styled-by-prettify">operator</span><span style=
=3D"color: #660;" class=3D"styled-by-prettify">=3D(</span><span style=3D"co=
lor: #000;" class=3D"styled-by-prettify">assignment_t</span><span style=3D"=
color: #660;" class=3D"styled-by-prettify">&amp;&amp;</span><span style=3D"=
color: #000;" class=3D"styled-by-prettify"> value</span><span style=3D"colo=
r: #660;" class=3D"styled-by-prettify">)</span><span style=3D"color: #000;"=
 class=3D"styled-by-prettify"> </span><span style=3D"color: #660;" class=3D=
"styled-by-prettify">{</span></div><div><span style=3D"color: #000;" class=
=3D"styled-by-prettify"> =C2=A0 =C2=A0 =C2=A0 =C2=A0func</span><span style=
=3D"color: #660;" class=3D"styled-by-prettify">(</span><span style=3D"color=
: #000;" class=3D"styled-by-prettify">std</span><span style=3D"color: #660;=
" class=3D"styled-by-prettify">::</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify">forward</span><span style=3D"color: #660;" class=3D=
"styled-by-prettify">&lt;</span><span style=3D"color: #008;" class=3D"style=
d-by-prettify">decltype</span><span style=3D"color: #660;" class=3D"styled-=
by-prettify">(</span><span style=3D"color: #000;" class=3D"styled-by-pretti=
fy">value</span><span style=3D"color: #660;" class=3D"styled-by-prettify">)=
&gt;(</span><span style=3D"color: #000;" class=3D"styled-by-prettify">value=
</span><span style=3D"color: #660;" class=3D"styled-by-prettify">));</span>=
</div><div><span style=3D"color: #000;" class=3D"styled-by-prettify"> =C2=
=A0 =C2=A0 =C2=A0 =C2=A0</span><span style=3D"color: #008;" class=3D"styled=
-by-prettify">return</span><span style=3D"color: #000;" class=3D"styled-by-=
prettify"> </span><span style=3D"color: #660;" class=3D"styled-by-prettify"=
>*</span><span style=3D"color: #008;" class=3D"styled-by-prettify">this</sp=
an><span style=3D"color: #660;" class=3D"styled-by-prettify">;</span></div>=
<div><span style=3D"color: #000;" class=3D"styled-by-prettify"> =C2=A0 =C2=
=A0</span><span style=3D"color: #660;" class=3D"styled-by-prettify">}</span=
></div><div><span style=3D"color: #660;" class=3D"styled-by-prettify">};</s=
pan></div></font></div></div></code></div></div></div></div><div><br></div>=
<div>A programmer could pass the lazy type to a function which might requir=
e an object that would otherwise be expensive to construct: if the function=
 doesn&#39;t require the object, it&#39;s never constructed, and if the fun=
ction does require it, the lazy type constructs and returns the object. Int=
ernally, the callable object stored in the lazy type could store a std::opt=
ional.=C2=A0</div><div><br></div><div>If class template argument deduction =
is allowed within the body of a class, lazy types could also be used to imp=
lement properties. A lazy_type could also be used during debugging or bench=
marking to count the number of accesses or assignments to a variable, and l=
azy_types stored in a vector would be a convenient way of solving problems =
where indirection is required.=C2=A0</div></div>

<p></p>

-- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals&quot; group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/e2995c19-feec-49d5-be8b-aca9ff08ac2e%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/e2995c19-feec-49d5-be8b-aca9ff08ac2e=
%40isocpp.org</a>.<br />

------=_Part_8600_1161028995.1527188510196--

------=_Part_8599_608758115.1527188510195--

.


Author: "'Matt Calabrese' via ISO C++ Standard - Future Proposals" <std-proposals@isocpp.org>
Date: Thu, 24 May 2018 16:49:34 -0400
Raw View
--0000000000009ee1c6056cf9ca3e
Content-Type: text/plain; charset="UTF-8"

On Thu, May 24, 2018 at 3:01 PM Antonio Perez <antonio@perezexcelsior.com>
wrote:

> Lazy evaluation is a powerful abstraction with a plethora of uses. While
> it traditionally appears in fully-functional languages, due to C++'s
> flexibility it's possible to implement lazy evaluation within C++ in a way
> that leverages the strengths of the language.
>
> Below is an example implementation done in C++17. Here lazy_t stores a
> callable object, which it invokes and returns when the implicit conversion
> operator is invoked.
>

Please show examples of intended usage, both on the caller side and in the
body of a function or function template that can take such a lazy_t.
Specifically, I don't see an advantage of this over simply having your
function take an Invocable returning a T as opposed to a T directly, and
the implementation you've shown here has some dubious design choices.

Related: if you want "lazy parameters", there is a language-level proposal
that might suit your needs ( http://wg21.link/p0927 ).

--
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CANh8DE%3DosEwquMrt63Q63X%3Dmo_Rt_3uhGnt19gfbJ4En66Lnzg%40mail.gmail.com.

--0000000000009ee1c6056cf9ca3e
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div class=3D"gmail_quote"><div dir=3D"ltr">On Thu, May 24=
, 2018 at 3:01 PM Antonio Perez &lt;<a href=3D"mailto:antonio@perezexcelsio=
r.com" target=3D"_blank">antonio@perezexcelsior.com</a>&gt; wrote:<br></div=
><blockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;border=
-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir=3D"ltr">Lazy ev=
aluation is a powerful abstraction with a plethora of uses. While it tradit=
ionally appears in fully-functional languages, due to C++&#39;s flexibility=
 it&#39;s possible to implement lazy evaluation within C++ in a way that le=
verages the strengths of the language.=C2=A0<div><br></div><div>Below is an=
 example implementation done in C++17. Here lazy_t stores a callable object=
, which it invokes and returns when the implicit conversion operator is inv=
oked.</div></div></blockquote><div><br></div><div>Please show examples of i=
ntended usage, both on the caller side and in the body of a function or fun=
ction template that can take such a lazy_t. Specifically, I don&#39;t see a=
n advantage of this over simply having your function take an Invocable retu=
rning a T as opposed to a T directly, and the implementation you&#39;ve sho=
wn here has some dubious design choices.</div><div><br></div><div>Related: =
if you want &quot;lazy parameters&quot;, there is a language-level proposal=
 that might suit your needs (=C2=A0<a href=3D"http://wg21.link/p0927">http:=
//wg21.link/p0927</a> ).</div></div></div>

<p></p>

-- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals&quot; group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/CANh8DE%3DosEwquMrt63Q63X%3Dmo_Rt_3uh=
Gnt19gfbJ4En66Lnzg%40mail.gmail.com?utm_medium=3Demail&utm_source=3Dfooter"=
>https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CANh8DE%3DosE=
wquMrt63Q63X%3Dmo_Rt_3uhGnt19gfbJ4En66Lnzg%40mail.gmail.com</a>.<br />

--0000000000009ee1c6056cf9ca3e--

.