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<cstdio><br>class f<br>{<br> std::FILE =
*fp;<br>public:<br> std::FILE* get(){return fp;}<br> f(const char=
*p):fp(std::fopen(p,"w")){}<br>//copy operations deleted<br> f(const =
f&)=3Ddelete;<br> f& operator=3D(const f&)=3Ddelete;</p><p=
>//move operations defined </p><p> f(f&& bmv) noexcept : fp(bm=
v.fp)<br> {<br> bmv.fp=3Dnullptr;<br> }<br> f& ope=
rator=3D(f&& bmv) noexcept<br> {<br> if(this!=3D&bmv=
)<br> {<br> fp=3Dbmv.fp;<br> bmv.fp=3Dnullptr=
;<br> }<br> }<br> <br> ~f(){fclose(fp);}<br>};</p><p>i=
nt main()<br>{<br> f a("324.txt");<br> 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<cstdio><br>cla=
ss f<br>{<br> std::FILE *fp;<br>public:<br> std::FILE* get(){retu=
rn fp;}<br> f(const char *p):fp(std::fopen(p,"w")){}<br>//copy operati=
ons deleted<br> f(const f&)=3Ddelete;<br> f& operator=3D(=
const f&)=3Ddelete;</p><p>//move operations defined<br> f(f&&a=
mp; bmv) noexcept =3Dauto;<br> f& operator=3D(f&&) noexcep=
t =3Dauto;<br> <br> ~f(){fclose(fp);}<br>};</p><p>int main()<br>{=
<br> f a("324.txt");<br> 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<f> 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. if(this!=3D&bmv)<br>2. =
bmv.fp=3Dnullptr;<br>3. allocator.deconstruct(*this);<br>4. allo=
cator.destory(this,1);</p><p>I just need to do:<br>1. memcpy(,,);<br>=
2. 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> f(f&& bmv) noexcept : fp(bmv.fp=
)<br> {<br> bmv.fp=3Dnullptr;<br> }<br> f& operato=
r=3D(f&& bmv) noexcept<br> {<br> if(this!=3D&bmv)<br=
> {<br> fp=3Dbmv.fp;<br> bmv.fp=3Dnullptr;<br=
> }<br> }<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 />
<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/">http://groups.google.com/a/isocpp.org/group/std-proposals/<=
/a>.<br />
------=_Part_136_27946837.1390527955876--
.