Topic: Refactoring of arithmetic operations


Author: Vlad from Moscow <vlad.moscow@mail.ru>
Date: Fri, 5 Apr 2013 00:40:22 -0700 (PDT)
Raw View
------=_Part_1581_25819326.1365147622847
Content-Type: text/plain; charset=ISO-8859-1

What about rewriting standard arithmetic operattions as for example

#include <iostream>
#include <functional>

template <class T, class R = T>
struct multiplies
{
 R operator ()( const T &x, const T &y ) const
 {
  return ( static_cast<R>( x ) * y );
 }
};

int main()
{

  int x = 10, y = 20;

  std::cout << x << " * " << y << " = " << multiplies<int>()( x, y ) <<
std::endl;
  std::cout << x << " * " << y << " = " << std::multiplies<int>()( x, y )
<< std::endl;

  x = INT_MAX, y = INT_MAX;

  std::cout << x << " * " << y << " = " << multiplies<int, long long>()( x,
y ) << std::endl;
  std::cout << x << " * " << y << " = " << std::multiplies<int>()( x, y )
<< std::endl;
}

--

---
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.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/?hl=en.



------=_Part_1581_25819326.1365147622847
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

<div>What about rewriting standard arithmetic operattions as for example</d=
iv><div>&nbsp;</div><div>#include &lt;iostream&gt;</div><div>#include &lt;f=
unctional&gt;</div><div>&nbsp;</div><div>template &lt;class T, class R =3D =
T&gt;</div><div>struct multiplies<br>{<br>&nbsp;R operator ()( const T &amp=
;x, const T &amp;y ) const<br>&nbsp;{<br>&nbsp;&nbsp;return ( static_cast&l=
t;R&gt;( x ) * y );<br>&nbsp;}<br>};<br>&nbsp;<br>int main()<br>{<br><br>&n=
bsp;&nbsp;int x =3D 10, y =3D 20;</div><div>&nbsp;</div><div>&nbsp;&nbsp;st=
d::cout &lt;&lt; x &lt;&lt; " * " &lt;&lt; y &lt;&lt; " =3D " &lt;&lt; mult=
iplies&lt;int&gt;()( x, y ) &lt;&lt; std::endl;<br>&nbsp; std::cout &lt;&lt=
; x &lt;&lt; " * " &lt;&lt; y &lt;&lt; " =3D " &lt;&lt; std::multiplies&lt;=
int&gt;()( x, y ) &lt;&lt; std::endl;<br>&nbsp;&nbsp;<br>&nbsp; x =3D INT_M=
AX, y =3D INT_MAX;</div><div>&nbsp;</div><div>&nbsp;&nbsp;std::cout &lt;&lt=
; x &lt;&lt; " * " &lt;&lt; y &lt;&lt; " =3D " &lt;&lt; multiplies&lt;int, =
long long&gt;()( x, y ) &lt;&lt; std::endl;</div><div>&nbsp;&nbsp;std::cout=
 &lt;&lt; x &lt;&lt; " * " &lt;&lt; y &lt;&lt; " =3D " &lt;&lt; std::multip=
lies&lt;int&gt;()( x, y ) &lt;&lt; std::endl;<br>}<br></div>

<p></p>

-- <br />
&nbsp;<br />
--- <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 std-proposals+unsubscribe@isocpp.org.<br />
To post to this group, send email to std-proposals@isocpp.org.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/?hl=3Den">http://groups.google.com/a/isocpp.org/group/std-pro=
posals/?hl=3Den</a>.<br />
&nbsp;<br />
&nbsp;<br />

------=_Part_1581_25819326.1365147622847--

.


Author: Vlad from Moscow <vlad.moscow@mail.ru>
Date: Fri, 5 Apr 2013 00:44:25 -0700 (PDT)
Raw View
------=_Part_1506_32177010.1365147865614
Content-Type: text/plain; charset=ISO-8859-1


On Friday, April 5, 2013 11:40:22 AM UTC+4, Vlad from Moscow wrote:
>
> What about rewriting standard arithmetic operattions as for example
>
> #include <iostream>
> #include <functional>
>
> template <class T, class R = T>
> struct multiplies
> {
>  R operator ()( const T &x, const T &y ) const
>  {
>   return ( static_cast<R>( x ) * y );
>  }
> };
>
> int main()
> {
>
>   int x = 10, y = 20;
>
>   std::cout << x << " * " << y << " = " << multiplies<int>()( x, y ) <<
> std::endl;
>   std::cout << x << " * " << y << " = " << std::multiplies<int>()( x, y )
> << std::endl;
>
>   x = INT_MAX, y = INT_MAX;
>
>   std::cout << x << " * " << y << " = " << multiplies<int, long long>()(
> x, y ) << std::endl;
>   std::cout << x << " * " << y << " = " << std::multiplies<int>()( x, y )
> << std::endl;
> }
>

Of course, standard C header <climits> has to be included in the example
above.:)

--

---
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.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/?hl=en.



------=_Part_1506_32177010.1365147865614
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

<br>On Friday, April 5, 2013 11:40:22 AM UTC+4, Vlad from Moscow wrote:<blo=
ckquote style=3D"margin: 0px 0px 0px 0.8ex; padding-left: 1ex; border-left-=
color: rgb(204, 204, 204); border-left-width: 1px; border-left-style: solid=
;" class=3D"gmail_quote"><div>What about rewriting standard arithmetic oper=
attions as for example</div><div>&nbsp;</div><div>#include &lt;iostream&gt;=
</div><div>#include &lt;functional&gt;</div><div>&nbsp;</div><div>template =
&lt;class T, class R =3D T&gt;</div><div>struct multiplies<br>{<br>&nbsp;R =
operator ()( const T &amp;x, const T &amp;y ) const<br>&nbsp;{<br>&nbsp;&nb=
sp;return ( static_cast&lt;R&gt;( x ) * y );<br>&nbsp;}<br>};<br>&nbsp;<br>=
int main()<br>{<br><br>&nbsp;&nbsp;int x =3D 10, y =3D 20;</div><div>&nbsp;=
</div><div>&nbsp;&nbsp;std::cout &lt;&lt; x &lt;&lt; " * " &lt;&lt; y &lt;&=
lt; " =3D " &lt;&lt; multiplies&lt;int&gt;()( x, y ) &lt;&lt; std::endl;<br=
>&nbsp; std::cout &lt;&lt; x &lt;&lt; " * " &lt;&lt; y &lt;&lt; " =3D " &lt=
;&lt; std::multiplies&lt;int&gt;()( x, y ) &lt;&lt; std::endl;<br>&nbsp;&nb=
sp;<br>&nbsp; x =3D INT_MAX, y =3D INT_MAX;</div><div>&nbsp;</div><div>&nbs=
p;&nbsp;std::cout &lt;&lt; x &lt;&lt; " * " &lt;&lt; y &lt;&lt; " =3D " &lt=
;&lt; multiplies&lt;int, long long&gt;()( x, y ) &lt;&lt; std::endl;</div><=
div>&nbsp;&nbsp;std::cout &lt;&lt; x &lt;&lt; " * " &lt;&lt; y &lt;&lt; " =
=3D " &lt;&lt; std::multiplies&lt;int&gt;()( x, y ) &lt;&lt; std::endl;<br>=
}<br></div></blockquote><div>&nbsp;</div><div>Of course, standard C header =
&lt;climits&gt;&nbsp;has to be included in the example above.:)&nbsp;</div>

<p></p>

-- <br />
&nbsp;<br />
--- <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 std-proposals+unsubscribe@isocpp.org.<br />
To post to this group, send email to std-proposals@isocpp.org.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/?hl=3Den">http://groups.google.com/a/isocpp.org/group/std-pro=
posals/?hl=3Den</a>.<br />
&nbsp;<br />
&nbsp;<br />

------=_Part_1506_32177010.1365147865614--

.


Author: =?ISO-8859-1?Q?Daniel_Kr=FCgler?= <daniel.kruegler@gmail.com>
Date: Fri, 5 Apr 2013 09:50:29 +0200
Raw View
--f46d043c7f1a47501604d9985707
Content-Type: text/plain; charset=ISO-8859-1

2013/4/5 Vlad from Moscow <vlad.moscow@mail.ru>

> What about rewriting standard arithmetic operattions as for example
>
> #include <iostream>
> #include <functional>
>
> template <class T, class R = T>
> struct multiplies
> {
>  R operator ()( const T &x, const T &y ) const
>  {
>   return ( static_cast<R>( x ) * y );
>  }
> };
>
> int main()
> {
>
>   int x = 10, y = 20;
>
>   std::cout << x << " * " << y << " = " << multiplies<int>()( x, y ) <<
> std::endl;
>   std::cout << x << " * " << y << " = " << std::multiplies<int>()( x, y )
> << std::endl;
>
>   x = INT_MAX, y = INT_MAX;
>
>   std::cout << x << " * " << y << " = " << multiplies<int, long long>()(
> x, y ) << std::endl;
>   std::cout << x << " * " << y << " = " << std::multiplies<int>()( x, y )
> << std::endl;
> }
>
> This looks like an unfortunate mixing of concerns to me. Attempting to
convert some value X to a different type Y should be done by a separate
layer, I see no good reason to mix this into the already separated "atomic"
function objects such as std::multiplies. You can easily construct a
template that takes an arbitrary function object of given arity that
converts the result to any other type. Further-more you can decide for
whether the conversion should be implicit, or via static_cast. I see no
reason why static_cast is the most natural one. To the contrary: Most
"natural" conversions are implicit and you find a lot of these all around
the library. Two notable examples are the overloaded INVOKE or the
overloaded std::bind.

- Daniel

--

---
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.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/?hl=en.



--f46d043c7f1a47501604d9985707
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div class=3D"gmail_extra"><div class=3D"gmail_quote">2013=
/4/5 Vlad from Moscow <span dir=3D"ltr">&lt;<a href=3D"mailto:vlad.moscow@m=
ail.ru" target=3D"_blank">vlad.moscow@mail.ru</a>&gt;</span><br><blockquote=
 class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;border-left:1px so=
lid rgb(204,204,204);padding-left:1ex">
<div>What about rewriting standard arithmetic operattions as for example</d=
iv><div>=A0</div><div>#include &lt;iostream&gt;</div><div>#include &lt;func=
tional&gt;</div><div>=A0</div><div>template &lt;class T, class R =3D T&gt;<=
/div>
<div>struct multiplies<br>{<br>=A0R operator ()( const T &amp;x, const T &a=
mp;y ) const<br>=A0{<br>=A0=A0return ( static_cast&lt;R&gt;( x ) * y );<br>=
=A0}<br>};<br>=A0<br>int main()<br>{<br><br>=A0=A0int x =3D 10, y =3D 20;</=
div><div>=A0</div>
<div>=A0=A0std::cout &lt;&lt; x &lt;&lt; &quot; * &quot; &lt;&lt; y &lt;&lt=
; &quot; =3D &quot; &lt;&lt; multiplies&lt;int&gt;()( x, y ) &lt;&lt; std::=
endl;<br>=A0 std::cout &lt;&lt; x &lt;&lt; &quot; * &quot; &lt;&lt; y &lt;&=
lt; &quot; =3D &quot; &lt;&lt; std::multiplies&lt;int&gt;()( x, y ) &lt;&lt=
; std::endl;<br>
=A0=A0<br>=A0 x =3D INT_MAX, y =3D INT_MAX;</div><div>=A0</div><div>=A0=A0s=
td::cout &lt;&lt; x &lt;&lt; &quot; * &quot; &lt;&lt; y &lt;&lt; &quot; =3D=
 &quot; &lt;&lt; multiplies&lt;int, long long&gt;()( x, y ) &lt;&lt; std::e=
ndl;</div><div>
=A0=A0std::cout &lt;&lt; x &lt;&lt; &quot; * &quot; &lt;&lt; y &lt;&lt; &qu=
ot; =3D &quot; &lt;&lt; std::multiplies&lt;int&gt;()( x, y ) &lt;&lt; std::=
endl;<br>}<span class=3D""><font color=3D"#888888"><br></font></span></div>=
<span class=3D""><font color=3D"#888888">

<p></p></font></span></blockquote><div>This looks like an unfortunate mixin=
g of concerns to me. Attempting to convert some value X to a different type=
 Y should be done by a separate layer, I see no good reason to mix this int=
o the already separated &quot;atomic&quot; function objects such as std::mu=
ltiplies. You can easily construct a template that takes an arbitrary funct=
ion object of given arity that converts the result to any other type. Furth=
er-more you can decide for whether the conversion should be implicit, or vi=
a static_cast. I see no reason why static_cast is the most natural one. To =
the contrary: Most &quot;natural&quot; conversions are implicit and you fin=
d a lot of these all around the library. Two notable examples are the overl=
oaded INVOKE or the overloaded std::bind.<br>
<br></div><div>- Daniel<br></div></div><br><br></div></div>

<p></p>

-- <br />
&nbsp;<br />
--- <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 std-proposals+unsubscribe@isocpp.org.<br />
To post to this group, send email to std-proposals@isocpp.org.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/?hl=3Den">http://groups.google.com/a/isocpp.org/group/std-pro=
posals/?hl=3Den</a>.<br />
&nbsp;<br />
&nbsp;<br />

--f46d043c7f1a47501604d9985707--

.