簡體   English   中英

雖然沒有使用,但需要C ++拷貝構造函數

[英]C++ copy constructor needed although not used

在這種情況下,任何人都知道為什么編譯器需要Foo的復制構造函數:

#include <iostream>
#include <list>

class Foo {
public:
  Foo() {}
  Foo(const Foo &&f) noexcept {}
  Foo(const Foo &f) = delete;
  ~Foo() {}
};

void passFoo2(const Foo&& f) {
  std::list<Foo> l;
  l.push_back(std::move(f));
}

int main(int argc, char **argv) {
  Foo f;
  passFoo2(std::move(f));
  return 0;
}

編譯器(g ++)抱怨復制構造函數被刪除。 但在那種情況下它不應該需要嗎?

那我在這里錯過了什么?

x@ubuntu:/tmp/c++$ g++ stackoverflow.cxx -std=c++11
In file included from /usr/include/c++/4.9/list:63:0,
             from stackoverflow.cxx:2:
/usr/include/c++/4.9/bits/stl_list.h: In instantiation of std::_List_node<_Tp>::_List_node(_Args&& ...) [with _Args = {const Foo&}; _Tp = Foo]:
/usr/include/c++/4.9/ext/new_allocator.h:120:4:   required from void __gnu_cxx::new_allocator<_Tp>::construct(_Up*, _Args&& ...) [with _Up = std::_List_node<Foo>; _Args = {const Foo&}; _Tp = std::_List_node<Foo>]
/usr/include/c++/4.9/bits/stl_list.h:514:8:   required from std::list<_Tp, _Alloc>::_Node* std::list<_Tp, _Alloc>::_M_create_node(_Args&& ...) [with _Args = {const Foo&}; _Tp = Foo; _Alloc = std::allocator<Foo>; std::list<_Tp, _Alloc>::_Node = std::_List_node<Foo>]
/usr/include/c++/4.9/bits/stl_list.h:1688:63:   required from void std::list<_Tp, _Alloc>::_M_insert(std::list<_Tp, _Alloc>::iterator, _Args&& ...) [with _Args = {const Foo&}; _Tp = Foo; _Alloc = std::allocator<Foo>; std::list<_Tp, _Alloc>::iterator = std::_List_iterator<Foo>]
/usr/include/c++/4.9/bits/stl_list.h:1029:9:   required from void std::list<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = Foo; _Alloc = std::allocator<Foo>; std::list<_Tp, _Alloc>::value_type = Foo]
stackoverflow.cxx:14:30:   required from here
/usr/include/c++/4.9/bits/stl_list.h:114:71: error: use of deleted function Foo::Foo(const Foo&)
  : __detail::_List_node_base(), _M_data(std::forward<_Args>(__args)...) 
                                                                   ^
stackoverflow.cxx:8:3: note: declared here
   Foo(const Foo &f) = delete;
   ^

原因是push_back有一個Foo&&的重載而不是const Foo&&

const Foo&&是多余的。 這是一種合法的類型,但它的存在是不合時宜的。

這編譯:

#include <iostream>
#include <list>

class Foo {
public:
    Foo() {}
    Foo( Foo &&f) noexcept {}
    Foo(const Foo &f) = delete;
    ~Foo() {}
};

void passFoo2(Foo&& f) {
    std::list<Foo> list;
    list.push_back(std::move(f));
}

int main(int argc, char **argv) {
    Foo f;
    passFoo2(std::move(f));
    return 0;
}

push_back方法有兩個重載:

void push_back( const T& value );
void push_back( T&& value );

如果你使用第一個, T (在你的情況下為Foo )必須是“CopyInsertable”。 如果使用第二個,則必須為“MoveInsertable”。

你的passFoo2函數接收一個const Foo&&引用,因為它是const -qualified(見下面的注釋),所以第一個重載是最佳匹配。 因此,調用該重載,這需要您的類Foo是可復制的。

注意:函數std::move變換一個命名對象使其匿名,因此,適合被rvalue-reference綁定,但它不會改變const限定(它是移動后的const Foo匿名對象) 。 因此,調用第一個重載。

暫無
暫無

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

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