C++11中使用std::move和copy and swap习惯用法时,'operator='的重载存在歧义。

9
我遇到了以下错误:
[matt ~] g++ -std=c++11 main.cpp -DCOPY_AND_SWAP && ./a.out
main.cpp: In function ‘int main(int, const char* const*)’:
main.cpp:101:24: error: ambiguous overload for ‘operator=’ in ‘move = std::move<Test&>((* & copy))’
main.cpp:101:24: note: candidates are:
main.cpp:39:7: note: Test& Test::operator=(Test)
main.cpp:52:7: note: Test& Test::operator=(Test&&)

当以下代码被编译时:
#include <iostream>
#include <unordered_map>
class Test final {
public:
  typedef std::unordered_map<std::string, std::string> Map;
public:
  Test();
  explicit Test(Map&& map);
  ~Test();
  Test(const Test& other);
  Test(Test&& test);
#ifdef COPY_AND_SWAP
  Test& operator=(Test other);
#else
  Test& operator=(const Test& other);
#endif
  Test& operator=(Test&& other);
  size_t Size() const noexcept;
  friend void swap(Test& lhs, Test& rhs);
private:
  friend std::ostream& operator<<(std::ostream& stream, const Test& test);
private:
  Map map_;
};
Test::Test() : map_() {
  std::cerr << "Default constructor called" << std::endl;
};
Test::Test(const Test& other) : map_(other.map_) {
  std::cerr << "Copy constructor called" << std::endl;
};
Test::Test(Test&& other) : map_(std::move(other.map_)) {
  std::cerr << "Move constructor called" << std::endl;
};
Test::Test(Map&& map) : map_(std::move(map)) {
  std::cerr << "Map constructor called" << std::endl;
};
Test::~Test() {};
#ifdef COPY_AND_SWAP
Test& Test::operator=(Test other) {
  std::cerr << "Copy and swap assignment called" << std::endl;
  using std::swap;
  swap(this->map_, other.map_);
  return *this;
}
#else
Test& Test::operator=(const Test& other) {
  std::cerr << "Copy assignment called" << std::endl;
  this->map_ = other.map_;
  return *this;
}
#endif
Test& Test::operator=(Test&& other) {
  std::cerr << "Move assignment called" << std::endl;
  this->map_ = other.map_;
  other.map_.clear();
  return *this;
}
size_t Test::Size() const noexcept {
  return map_.size();
}
void swap(Test& lhs, Test& rhs) {
  using std::swap;
  swap(lhs.map_, rhs.map_);
}
std::ostream& operator<<(std::ostream& stream, const Test& test) {
  return stream << test.map_.size();
}
int main (const int argc, const char * const * const argv) {
  using std::swap;
  Test::Map map {
    {"some", "dummy"},
    {"data", "to"},
    {"fill", "up"},
    {"the", "map"}
  };
  std::cout << " map size(): " << map.size() << std::endl;
  std::cout << "Constructing" << std::endl;
  Test test(std::move(map));
  std::cout << " map.size(): " << map.size() << std::endl;
  std::cout << "test.Size(): " << test.Size() << std::endl;
  std::cout << "Copy construction" << std::endl;
  Test copy(test);
  std::cout << "copy.Size(): " << copy.Size() << std::endl;
  std::cout << "Move construction" << std::endl;
  Test move(std::move(copy));
  std::cout << "move.Size(): " << move.Size() << std::endl;
  std::cout << "copy.Size(): " << copy.Size() << std::endl;
  std::cout << "Swapping" << std::endl;
  swap(move, copy);
  std::cout << "move.Size(): " << move.Size() << std::endl;
  std::cout << "copy.Size(): " << copy.Size() << std::endl;
  std::cout << "Swapping back" << std::endl;
  swap(move, copy);
  std::cout << "move.Size(): " << move.Size() << std::endl;
  std::cout << "copy.Size(): " << copy.Size() << std::endl;
  std::cout << "Copy assignment" << std::endl;
  copy = test;
  std::cout << "test.Size(): " << test.Size() << std::endl;
  std::cout << "copy.Size(): " << copy.Size() << std::endl;
  std::cout << "Move assignment" << std::endl;
  move = std::move(copy);
  std::cout << "move.Size(): " << move.Size() << std::endl;
  std::cout << "copy.Size(): " << copy.Size() << std::endl;
 return 0;
}

使用g++ -std=c++11 main.cpp && ./a.out编译时:

[matt ~] g++ -std=c++11 main.cpp && ./a.out
 map size(): 4
Constructing
Map constructor called
 map.size(): 0
test.Size(): 4
Copy construction
Copy constructor called
copy.Size(): 4
Move construction
Move constructor called
move.Size(): 4
copy.Size(): 0
Swapping
move.Size(): 0
copy.Size(): 4
Swapping back
move.Size(): 4
copy.Size(): 0
Copy assignment
Copy assignment called
test.Size(): 4
copy.Size(): 4
Move assignment
Move assignment called
move.Size(): 4
copy.Size(): 0

有人能帮我理解为什么在这种情况下使用复制并交换惯用语时会出现歧义吗?

1个回答

12

出于重载解析的目的,函数

Test& operator=(Test other);
Test& operator=(Test&& other);

它们是相等的,因为用于将其分别转换为Test和Test&&的隐式转换序列相等。前者并不更好,因为直接引用绑定也被认为是一种标识转换。

当遇到两个同样好的匹配的歧义时,编译器会报错。您可能想要这个:

#ifdef COPY_AND_SWAP
  Test& operator=(Test other);
#else
  Test& operator=(const Test& other);
  Test& operator=(Test&& other);
#endif

4
问题的解决方案是摆脱移动赋值运算符,而使用拷贝交换函数来处理右值和左值,这就是关键。 - Jonathan Wakely
@rhalbersma 感谢您的回答。显然,我需要更多地了解为什么隐式转换序列通过更好地理解 rvalues 是相同的。 - Matt Clarkson

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接