std::cout<<n+n<<std::endl;
}
};
is T{5} a temporary object or an explicit constructor call?
Is this question has a curse or am too bad at asking questions?
There won't be any temporary as C++ 17 guarantees copy elision
Copy elision is avoiding unnecessary copying right? But what it has to do with object creation?
Copy elision is avoiding unnecessary copy and move. There used to be issues when there was no guaranteed copy elision. For example, T t = f(); This type of expression would create a temporary which would be copied before C++11 afaik. Then later C++ standards gave you the ability to extend the lifetime of the temporaries for the sake of moving them. Now with guaranteed copy elision, the object is constructed in place rather than creating an unnecessarily copy
So it's only an explicit call?
What do you mean by that?
Here they said that calls like vector(2) are explicit (page: 643)
Okay, I misunderstood what you meant to ask T{5}, can invoke the copy constructor but this can be elided. Gimme a moment so I can read the specific passage. Don't forget to specify the name of the book when you're asking here. It may be obvious to me that you're referring to Programming:Practice and principles using C++ by Bjarne Stroustup but it's not to other members.
Here Bjarne is talking about implicit and explicit conversions. As for your question, T{5} will call the T(int) constructor directly so there's no disambiguity there.
it's an explicit call only?
I would say yes T{5} is you doing an explicit conversion, like T(5) T t = 5; This would be an implicit conversion
As for what I meant by eliding the copy construction T t = T{5}; The copy construction here will be elided. There will be only one call to T(int), you won't see T(T&)
It's all clear now 👍
Обсуждают сегодня