簡體   English   中英

為什么在 C++11 或 C++14 中,當我聲明移動賦值運算符時,編譯器會隱式刪除復制構造函數?

[英]Why in C++11 or C++14 does the compiler implicitly delete the copy constructor when I declare a move assignment operator?

我想創建一個包含迭代器類的列表數據結構。 一切正常,但是當我聲明移動賦值運算符時,如果程序使用 C++14 或 C++11 標准,則該程序不會編譯,但在 C++17、C++2a 中運行良好。

列表.h:

#pragma once

#include <iostream>

template <typename T>
class list {
    struct node {
        node(T data, node* prev = nullptr, node* next = nullptr)
            : data{ data }, prev{ prev }, next{ next } {}
        
        T data;
        node* prev;
        node* next;
    };
public:
    struct iterator {
        template <typename>
        friend class list;

        explicit iterator(node *_node = nullptr) 
            : _node(_node) {}

        iterator& operator=(iterator const &it) {
            _node = it._node;
        
            return *this;
        }
       
        iterator& operator=(iterator &&it) {
            // does nothing
            return *this;
        }

        T& operator*() {
            return _node->data;
        }

    private:
        node *_node;
    };

    list(T data) {
        Head = Tail = new node(data);

        size = 1;
    }

    iterator begin() {
        return iterator(Head);
    }
private:
    node* Head;
    node* Tail;

    int size;
};

主.cpp:

#include "list.h"

int main() {

    list<int> lst(100);

    std::cout << *lst.begin() << std::endl;

}

這是一個精簡版,但足以描述問題。

編譯器錯誤和注意消息:

error: use of deleted function ‘constexpr list::iterator::iterator(const list::iterator&)’
     return iterator(Head);
                         ^

note: ‘constexpr list::iterator::iterator(const list::iterator&)’ is implicitly declared as
       deleted because ‘list::iterator’ declares a move constructor or move assignment operator
     struct iterator {
        ^~~~~~~~
return iterator(Head);

為什么這在 C++17 中有效是因為在 C++17 中這是保證復制省略 這里沒有復制或移動。

在 return 語句中,當操作數是與函數返回類型相同類類型(忽略 cv 限定)的純右值時:

在 C++17 之前,這需要一個構造函數,但定義的移動賦值運算符刪除了它,在 C++17 中也刪除了它,但復制省略不需要它(同上):

復制/移動構造函數不需要存在或可訪問

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM