C++ move测试

c++ move测试

目的:测试一下move是不是真的按照预期被调用

结论:当然是跟预期一样了!

代码

#include <stdio.h>
#include <vector>

class Buffer
{
public:
    Buffer() { printf(__FUNCTION__ "\n"); }
    ~Buffer() { printf(__FUNCTION__ "\n"); }
    Buffer(Buffer const &) { printf(__FUNCTION__ "\n"); }
    Buffer &operator=(Buffer const &)
    {
        printf(__FUNCTION__ "\n");
        return *this;
    }

#if 0
    Buffer(Buffer &&)
    {
        printf("move" __FUNCTION__ "\n");
    }
    Buffer &operator=(Buffer &&)
    {
        printf("move" __FUNCTION__ "\n");
        return *this;
    }
#endif
};

void main()
{
    std::vector<Buffer> v;
    v.push_back((Buffer()));
}

/*
 * No move operator
Buffer::Buffer
Buffer::Buffer
Buffer::~Buffer
Buffer::~Buffer
 */

/*
 * With move operator
Buffer::Buffer
moveBuffer::Buffer
Buffer::~Buffer
Buffer::~Buffer
 */

Powered by Jekyll and Theme by solid

本站总访问量