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> </div><div>#include <iostream></div><div>#include <f=
unctional></div><div> </div><div>template <class T, class R =3D =
T></div><div>struct multiplies<br>{<br> R operator ()( const T &=
;x, const T &y ) const<br> {<br> return ( static_cast&l=
t;R>( x ) * y );<br> }<br>};<br> <br>int main()<br>{<br><br>&n=
bsp; int x =3D 10, y =3D 20;</div><div> </div><div> st=
d::cout << x << " * " << y << " =3D " << mult=
iplies<int>()( x, y ) << std::endl;<br> std::cout <<=
; x << " * " << y << " =3D " << std::multiplies<=
int>()( x, y ) << std::endl;<br> <br> x =3D INT_M=
AX, y =3D INT_MAX;</div><div> </div><div> std::cout <<=
; x << " * " << y << " =3D " << multiplies<int, =
long long>()( x, y ) << std::endl;</div><div> std::cout=
<< x << " * " << y << " =3D " << std::multip=
lies<int>()( x, y ) << std::endl;<br>}<br></div>
<p></p>
-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" 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 />
<br />
<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> </div><div>#include <iostream>=
</div><div>#include <functional></div><div> </div><div>template =
<class T, class R =3D T></div><div>struct multiplies<br>{<br> R =
operator ()( const T &x, const T &y ) const<br> {<br> &nb=
sp;return ( static_cast<R>( x ) * y );<br> }<br>};<br> <br>=
int main()<br>{<br><br> int x =3D 10, y =3D 20;</div><div> =
</div><div> std::cout << x << " * " << y <&=
lt; " =3D " << multiplies<int>()( x, y ) << std::endl;<br=
> std::cout << x << " * " << y << " =3D " <=
;< std::multiplies<int>()( x, y ) << std::endl;<br> &nb=
sp;<br> x =3D INT_MAX, y =3D INT_MAX;</div><div> </div><div>&nbs=
p; std::cout << x << " * " << y << " =3D " <=
;< multiplies<int, long long>()( x, y ) << std::endl;</div><=
div> std::cout << x << " * " << y << " =
=3D " << std::multiplies<int>()( x, y ) << std::endl;<br>=
}<br></div></blockquote><div> </div><div>Of course, standard C header =
<climits> has to be included in the example above.:) </div>
<p></p>
-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" 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 />
<br />
<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"><<a href=3D"mailto:vlad.moscow@m=
ail.ru" target=3D"_blank">vlad.moscow@mail.ru</a>></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 <iostream></div><div>#include <func=
tional></div><div>=A0</div><div>template <class T, class R =3D T><=
/div>
<div>struct multiplies<br>{<br>=A0R operator ()( const T &x, const T &a=
mp;y ) const<br>=A0{<br>=A0=A0return ( static_cast<R>( 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 << x << " * " << y <<=
; " =3D " << multiplies<int>()( x, y ) << std::=
endl;<br>=A0 std::cout << x << " * " << y <&=
lt; " =3D " << std::multiplies<int>()( x, y ) <<=
; std::endl;<br>
=A0=A0<br>=A0 x =3D INT_MAX, y =3D INT_MAX;</div><div>=A0</div><div>=A0=A0s=
td::cout << x << " * " << y << " =3D=
" << multiplies<int, long long>()( x, y ) << std::e=
ndl;</div><div>
=A0=A0std::cout << x << " * " << y << &qu=
ot; =3D " << std::multiplies<int>()( x, y ) << 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 "atomic" 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 "natural" 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 />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" 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 />
<br />
<br />
--f46d043c7f1a47501604d9985707--
.