lundi 25 mai 2020

Why is the constructor with universal reference not being used or ignored?

For the code below, I would expect the standard output to be a empty default constructed string.

How is the actual output of a.get() the string if all means to modify m_x through the constructor are disabled in this fashion?

#include <iostream>
#include <string>

class Test {
public:
  Test() = delete;
  explicit Test(const std::string& x) { m_x = x; }
  explicit Test(const Test&) = delete;
  explicit Test(Test&& rhs) {  }
  Test& operator=(const Test&) = delete;
  Test& operator=(Test&&) = delete;
  std::string get() const { return m_x; }

private:
  std::string m_x;
};

int main(int argc, char** argv)
{
  auto a(Test("tetst"));
  std::cout << a.get() << std::endl;
  return 0;
}

Aucun commentaire:

Enregistrer un commentaire