Topic: I think many methods could be written by compilers,


Author: euloanty@live.com
Date: Thu, 23 Jan 2014 17:45:55 -0800 (PST)
Raw View
------=_Part_136_27946837.1390527955876
Content-Type: text/plain; charset=UTF-8



1.move operations

In C++11 we introduced move operations. But in fact move operations could
be faster, for its meaning is almost move.

#include<cstdio>
class f
{
 std::FILE *fp;
public:
 std::FILE* get(){return fp;}
 f(const char *p):fp(std::fopen(p,"w")){}
//copy operations deleted
 f(const f&)=delete;
 f& operator=(const f&)=delete;

//move operations defined

 f(f&& bmv) noexcept : fp(bmv.fp)
 {
  bmv.fp=nullptr;
 }
 f& operator=(f&& bmv) noexcept
 {
  if(this!=&bmv)
  {
   fp=bmv.fp;
   bmv.fp=nullptr;
  }
 }

 ~f(){fclose(fp);}
};

int main()
{
 f a("324.txt");
 return 0;
}


But I think move operations are stupid, for the codes are very similar
between different uses.
When I write those operations, I often make mistakes to do these things,
and it's difficult to change and maintain.

I think this code could be replaced as

#include<cstdio>
class f
{
 std::FILE *fp;
public:
 std::FILE* get(){return fp;}
 f(const char *p):fp(std::fopen(p,"w")){}
//copy operations deleted
 f(const f&)=delete;
 f& operator=(const f&)=delete;

//move operations defined
 f(f&& bmv) noexcept =auto;
 f& operator=(f&&) noexcept =auto;

 ~f(){fclose(fp);}
};

int main()
{
 f a("324.txt");
 return 0;
}


Of course. this replace has its conditions.
It needs all its members are "auto-moved".

You may say: why you think this will bring efficiency?

You know we will have concept. So containers could find this type is
"auto-moved", they can directly use memcpy,memmove to optimize it.
And many no need operations could be optimized.

std::vector<f> vec;
vec.emplace_back("32.txt"):
When it do such things, it could directly called memcpy, and former
resources could directly be destory, and I don't need to do the following
sentenses:
1.  if(this!=&bmv)
2.  bmv.fp=nullptr;
3.  allocator.deconstruct(*this);
4. allocator.destory(this,1);

I just need to do:
1.  memcpy(,,);
2.  allocator.destory(vec.data(),vec.size());

This will be very very fast.
All our containers could be "auto-moved", so they can all be faster.

Of course, when you directly called moved. It's still:
//move operations defined

 f(f&& bmv) noexcept : fp(bmv.fp)
 {
  bmv.fp=nullptr;
 }
 f& operator=(f&& bmv) noexcept
 {
  if(this!=&bmv)
  {
   fp=bmv.fp;
   bmv.fp=nullptr;
  }
 }
2.
swap can be automatic generated too.
3.
+,-,*,/ ....They can all auto generated +=,-=,*=,/=
i++.
Those operations could make class types closer to internal types

--

---
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/.

------=_Part_136_27946837.1390527955876
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><p>1.move operations</p><p>In C++11 we introduced move ope=
rations. But in fact move operations could be faster, for its meaning is al=
most move.</p><p>#include&lt;cstdio&gt;<br>class f<br>{<br>&nbsp;std::FILE =
*fp;<br>public:<br>&nbsp;std::FILE* get(){return fp;}<br>&nbsp;f(const char=
 *p):fp(std::fopen(p,"w")){}<br>//copy operations deleted<br>&nbsp;f(const =
f&amp;)=3Ddelete;<br>&nbsp;f&amp; operator=3D(const f&amp;)=3Ddelete;</p><p=
>//move operations defined </p><p>&nbsp;f(f&amp;&amp; bmv) noexcept : fp(bm=
v.fp)<br>&nbsp;{<br>&nbsp; bmv.fp=3Dnullptr;<br>&nbsp;}<br>&nbsp;f&amp; ope=
rator=3D(f&amp;&amp; bmv) noexcept<br>&nbsp;{<br>&nbsp; if(this!=3D&amp;bmv=
)<br>&nbsp; {<br>&nbsp;&nbsp; fp=3Dbmv.fp;<br>&nbsp;&nbsp; bmv.fp=3Dnullptr=
;<br>&nbsp; }<br>&nbsp;}<br>&nbsp;<br>&nbsp;~f(){fclose(fp);}<br>};</p><p>i=
nt main()<br>{<br>&nbsp;f a("324.txt");<br>&nbsp;return 0;<br>}</p><p><br><=
/p><p>But I think move operations are stupid, for the codes are very simila=
r between different uses.<br>When I write those operations, I often make mi=
stakes to do these things, and it's difficult to change and maintain.</p><p=
>I think this code could be replaced as</p><p>#include&lt;cstdio&gt;<br>cla=
ss f<br>{<br>&nbsp;std::FILE *fp;<br>public:<br>&nbsp;std::FILE* get(){retu=
rn fp;}<br>&nbsp;f(const char *p):fp(std::fopen(p,"w")){}<br>//copy operati=
ons deleted<br>&nbsp;f(const f&amp;)=3Ddelete;<br>&nbsp;f&amp; operator=3D(=
const f&amp;)=3Ddelete;</p><p>//move operations defined<br>&nbsp;f(f&amp;&a=
mp; bmv) noexcept =3Dauto;<br>&nbsp;f&amp; operator=3D(f&amp;&amp;) noexcep=
t =3Dauto;<br>&nbsp;<br>&nbsp;~f(){fclose(fp);}<br>};</p><p>int main()<br>{=
<br>&nbsp;f a("324.txt");<br>&nbsp;return 0;<br>}</p><p><br></p><p>Of cours=
e. this replace has its conditions.<br>It needs all its members are "auto-m=
oved".</p><p>You may say: why you think this will bring efficiency?</p><p>Y=
ou know we will have concept. So containers could find this type is "auto-m=
oved", they can directly use memcpy,memmove to optimize it.<br>And many no =
need operations could be optimized.</p><p>std::vector&lt;f&gt; vec;<br>vec.=
emplace_back("32.txt"):<br>When it do such things, it could directly called=
 memcpy, and former resources could directly be destory, and I don't need t=
o do the following sentenses:<br>1.&nbsp; if(this!=3D&amp;bmv)<br>2.&nbsp; =
bmv.fp=3Dnullptr;<br>3.&nbsp; allocator.deconstruct(*this);<br>4.&nbsp;allo=
cator.destory(this,1);</p><p>I just need to do:<br>1.&nbsp; memcpy(,,);<br>=
2.&nbsp; allocator.destory(vec.data(),vec.size());</p><p>This will be very =
very fast.<br>All our containers could be "auto-moved", so they can all be =
faster.</p><p>Of course, when you directly called moved. It's still:<br>//m=
ove operations defined </p><p>&nbsp;f(f&amp;&amp; bmv) noexcept : fp(bmv.fp=
)<br>&nbsp;{<br>&nbsp; bmv.fp=3Dnullptr;<br>&nbsp;}<br>&nbsp;f&amp; operato=
r=3D(f&amp;&amp; bmv) noexcept<br>&nbsp;{<br>&nbsp; if(this!=3D&amp;bmv)<br=
>&nbsp; {<br>&nbsp;&nbsp; fp=3Dbmv.fp;<br>&nbsp;&nbsp; bmv.fp=3Dnullptr;<br=
>&nbsp; }<br>&nbsp;}<br>2.<br>swap can be automatic generated too.<br>3.<br=
>+,-,*,/ ....They can all auto generated +=3D,-=3D,*=3D,/=3D<br>i++.<br>Tho=
se operations could make class types closer to internal types</p></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/">http://groups.google.com/a/isocpp.org/group/std-proposals/<=
/a>.<br />

------=_Part_136_27946837.1390527955876--

.