【发布时间】:2016-05-09 16:44:53
【问题描述】:
这些是 Same 函数的定义,但第一个使用 move(&&) 传递参数,第二个使用 (const &) 传递。
template<typename T, class Allocator>
void MyList<T, Allocator>::push_front(T && t)
{
Link<T>* newnode = new Link<T>(t);
if (empty()) {
head = std::move(newnode);
tail = std::move(newnode);
std::cout << "Linked List Created using move with value: " <<t<< std::endl;
}
else {
head->prev = std::move(newnode);
newnode->next = std::move(head);
head = std::move(newnode);
std::cout << "Node Inserted using move at the beginning of List with value: " <<t<< std::endl;
}
}
template<typename T, class Allocator>
void MyList<T, Allocator>::push_front(const T & t)
{ Link<T>* newnode = new Link<T>(t);
if (empty()) {
head = newnode;
tail = newnode;
std::cout << "Linked List Created with value: " <<t<< std::endl;
}
else {
head->prev = newnode;
newnode->next = head;
head = newnode;
std::cout << "Node Inserted at the beginning of List with value: " <<t<< std::endl;
}
}
当我在 main 中运行以下代码时:
MyList<int> l1;
l1.push_front(5);
l1.push_front(std::move(8));
我总是得到move 函数的cout。 const & 不应该是默认的吗?
【问题讨论】:
-
std::move(new Link<T>(t))wtf 这应该怎么做?其他动作? -
@Slava 是的,你的权利,我在每个地方都添加了移动,我打算修改它但我忘了。感谢您的提醒!
-
编程不是靠猜测来工作的,你应该明白自己在做什么,而不是盲目地到处移动
-
注意
std::move不会移动任何东西,它是一个值类别转换 -
你应该先阅读这个isocpp.org/blog/2012/11/…
标签: c++ c++11 visual-c++ c++14