For Node<T> move constructor, T copy constructor is called when the move constructor is not defined but the move assignment operator is defined. #345
Labels
bug
Something isn't working
core
something about core
performance
Performance issue
Priority:Critical
Priority Label for Critical Issue
Describe the bug
For
Node<T>
, whenNode<T>::Node(const std::string& id, T&& data)
is invoked for T, where T's move constructor is not defined but T's move assignment operator is defined, the T's copy constructor is invoked.Hence where a move operation can be used, a copy operation is used.
A move operation is of constant time complexity, whereas a copy operation is of linear time complexity
To Reproduce (Minimal Reproducable Example)
Steps to reproduce the behavior:
Observed behavior
The output uses a copy constructor.
Expected behavior
The output should only use the move assignment operator.
Reason it happened
in
Node<T>::Node(const std::string& id, T&& data)
astd::swap()
is used.According to cpprefrence,
std::swap()
usesmove constructor
andmove assignment operator
both.So when
move constructor
is not defined, it shifts back tocopy constructor
.hence, defeating the purpose of
move
.How to resolve?
Use
std::move()
inNode<T>::Node(const std::string& id, T&& data)
instead, so even only ifmove assignment operator
is defined,move
can take place.Thanks.
The text was updated successfully, but these errors were encountered: