• C++ Rvalue Reference的一些链接的记载


    好文推荐与摘录:
    http://www.cprogramming.com/c++11/rvalue-references-and-move-semantics-in-c++11.html
     
    关于C++ Rvalue Reference 和 std::move() 的讲解.  讲解得非常透彻,真可谓“循循善诱”。

    1 string getName ()
    2 {
    3     return "Alex";
    4 }
    5 getName();
    6 
    7 
    8 const string& name = getName(); // ok
    9 string& name = getName(); // NOT ok
     
    "holding on to a const reference to a temporary object ensures that the temporary object isn't immediately destructed. This is a nice guarantee of C++, but it is still a temporary object, so you don't want to modify it"
     
    "both lvalue and rvalue references are lvalue expressions"
     
    "std::move does not, in and of itself, move anything; it just turns an lvalue into an rvalue, so that you can invoke the move constructor."
    "You might be wondering, how does one write a function like std::move? How do you get this magical property of transforming an lvalue into an rvalue reference? The answer, as you might guess, is typecastin . The actual declaration for std::move is somewhat more involved, but at its heart, it's just a static_cast to an rvalue reference."
     
    有一个地方貌似有点错误,就是

    Returning an explicit rvalue-reference from a function

    这一段

    On the other hand,

    1 printAddress( getRvalueInt() );
    2 printAddress( x );

    prints the same value because we are explicitly returning an rvalue here.

    应该改成:

    On the other hand,

    1 printAddress( getRvalueInt() );
    2 printAddress( x );

    prints the same value because we are explicitly returning an rvalue reference here.

    (少了一个reference)

           我一直认为,一个人如果对某些知识有了真真正正的理解,而且愿意写(讲),那么写(讲)出来的就一定是像这种文章一样的 “循循善诱” 式

    的“深入剖析”。因为如果真的很了解,那么就一定会知道一个知识点的关键所在,知道这个东西的细微之处,和与其他东西的本质区别,也就可以

    讲好,讲懂,而不是列出一堆别人不知所云,自己也不一定完全清楚的东西。

           而且,这种细致入微的剖析,其实也是对自己知识体系的一种重建,通过这个过程,自己也会对那一知识有更深入的了解。

           不过嘛,写起来确实挺麻烦的,还要考虑排版(有时候还要画讲解图),所以有时候就只能去头去尾了......

     再贴两篇:

    蓝色 的 关于RVO vs std::move()的文章:

    https://www.ibm.com/developerworks/community/blogs/5894415f-be62-4bc0-81c5-3956e82276f3/entry/RVO_V_S_std_move?lang=en

    github上一个很赞的C++模板教程,持续更新中:

    https://github.com/wuye9036/CppTemplateTutorial

    简直Nice!

  • 相关阅读:
    TCMalloc 内存分配原理简析
    技术人沟通中的几个常见问题
    不同路径
    Js将字符串转数字的方式
    防抖节流模式
    Location对象
    React生命周期
    fgrep命令
    数据访问对象模式
    保持城市天际线
  • 原文地址:https://www.cnblogs.com/walkerlala/p/5053654.html
Copyright © 2020-2023  润新知