I have this code
class MyString {
public:
MyString();
MyString(const char*);
MyString(const String&);
MyString(String&&) noexcept;
...
};
String::String()
{
std::cout << "default construct!" <<std::endl;
}
String::String(const char* cb)
{
std::cout << "construct with C-char!" <<std::endl;
...
}
String::String(const String& str)
{
std::cout << "copy construct!" <<std::endl;
...
}
String::String(String&& str) noexcept
{
std::cout << "move construct!" <<std::endl;
...
}
In main()
MyString s1(MyString("test"));
I thought result would be this:
construct with C-char! <---- called by MyString("test")
move construct! <---- called by s1(...)
But what I get was this:
construct with C-char! <---- maybe called by MyString()
The steps what I thought
MyString("test")
construct a rvalue by using the constructor withchar*
- Construct s1(arg)
- Because of arg is a rvalue, s1 should construct by move constructor But I find move constructor won't be called without
std::move
.
Why is that happening?
How to use move constructor without std::move()
?
Compiler:
Gnu C++ 9.3.0
Aucun commentaire:
Enregistrer un commentaire