测试新的移动语义。

我只是问了我对MOVE构造函数遇到的问题。但是,当您使用标准“副本和交换” idiom时,问题实际上是“移动分配”操作员和“标准分配”操作员冲突。

这是我正在使用的课程:

#include <string.h>
#include <utility>

class String
{
    int         len;
    char*       data;

    public:
        // Default constructor
        // In Terms of C-String constructor
        String()
            : String("")
        {}

        // Normal constructor that takes a C-String
        String(char const* cString)
            : len(strlen(cString))
            , data(new char[len+1]()) // Allocate and zero memory
        {
            memcpy(data, cString, len);
        }

        // Standard Rule of three
        String(String const& cpy)
            : len(cpy.len)
            , data(new char[len+1]())
        {
            memcpy(data, cpy.data, len);
        }
        String& operator=(String rhs)
        {
            rhs.swap(*this);
            return *this;
        }
        ~String()
        {
            delete [] data;
        }
        // Standard Swap to facilitate rule of three
        void swap(String& other) throw ()
        {
            std::swap(len,  other.len);
            std::swap(data, other.data);
        }

        // New Stuff
        // Move Operators
        String(String&& rhs) throw()
            : len(0)
            , data(null)
        {
            rhs.swap(*this);
        }
        String& operator=(String&& rhs) throw()
        {
            rhs.swap(*this);
            return *this;
        }
};

我认为漂亮的沼泽标准。

然后我像这样测试了我的代码:

int main()
{
    String  a("Hi");
    a   = String("Test Move Assignment");
}

这里的作业 a 应该使用“移动分配”操作员。但是,与“标准分配”操作员发生冲突(作为标准副本和交换而写)。

> g++ --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/usr/include/c++/4.2.1
Apple LLVM version 5.0 (clang-500.2.79) (based on LLVM 3.3svn)
Target: x86_64-apple-darwin13.0.0
Thread model: posix

> g++ -std=c++11 String.cpp
String.cpp:64:9: error: use of overloaded operator '=' is ambiguous (with operand types 'String' and 'String')
    a   = String("Test Move Assignment");
    ~   ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
String.cpp:32:17: note: candidate function
        String& operator=(String rhs)
                ^
String.cpp:54:17: note: candidate function
        String& operator=(String&& rhs)
                ^

现在,我可以通过将“标准分配”运算符修改为:

    String& operator=(String const& rhs)
    {
        String copy(rhs);
        copy.swap(*this);
        return *this;
    }

但这不是很好,因为它弄乱了编译器优化副本和交换的能力。查看什么是复制和交换成语? 这里这里

我是否错过了不太明显的东西?

有帮助吗?

解决方案

如果将分配运算符定义为占值,则不应(不需要也不能)定义分配运算符进行rvalue-reference。没有意义。

通常,您只需要在需要区分LVALUE与RVALUE时提供过载来进行重载,但是在这种情况下,您选择实施意味着您无需进行区分。无论您是有lvalue还是rvalue,您都将创建参数并交换内容。

String f();
String a;
a = f();   // with String& operator=(String)

在这种情况下,编译器将解决呼叫 a.operator=(f()); 它将意识到,返回值的唯一原因是参数 operator= 并将出现任何副本 - 这是使该函数首先获得值的重点!

其他提示

其他答案建议只有一个过载 operator =(String rhs) 按价值计数,但这是 不是 最有效的实施。

的确,在此示例中,戴维·罗德里格斯(DavidRodríguez)

String f();
String a;
a = f();   // with String& operator=(String)

没有副本。但是,假设 operator =(String rhs) 提供并考虑此示例:

String a("Hello"), b("World");
a = b;

发生的是

  1. b 被复制到 rhs (内存分配 + memcpy);
  2. arhs 被交换;
  3. rhs 被摧毁。

如果我们实施 operator =(const String& rhs)operator =(String&& rhs) 然后,当目标的长度大于源时,我们可以避免步骤1中的内存分配。例如,这是一个简单的实现(不完美:如果 String 有一个 capacity 成员):

String& operator=(const String& rhs) {
    if (len < rhs.len) {
        String tmp(rhs);
        swap(tmp);
    else {
        len = rhs.len;
        memcpy(data, rhs.data, len);
        data[len] = 0;
    }
    return *this;
}

String& operator =(String&& rhs) {
    swap(rhs);
}

除了性能点 swapnoexcept, , 然后 operator =(String&&)noexcept 也是。 (如果记忆分配是“潜在的”执行的,情况并非如此。)

在这个优秀的情况下查看更多细节 解释 霍华德·辛南特(Howard Hinnant)。

您需要的复制和分配所需的只是以下内容:

    // As before
    String(const String& rhs);

    String(String&& rhs)
    :   len(0), data(0)
    {
        rhs.swap(*this);
    }

    String& operator = (String rhs)
    {
        rhs.swap(*this);
        return *this;
    }

   void swap(String& other) noexcept {
       // As before
   }
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top