给定一个计划:警告有关调用构造函数复制时的std :: move'ing
#include <iostream>
#include <vector>
using namespace std;
struct Vec {
Vec() = default;
Vec(const Vec& vec)
: v_(vec.v_)
{
cout << "copy Vec" << endl;
}
Vec(Vec&& vec)
: v_(std::move(vec.v_))
{
cout << "move Vec" << endl;
}
vector<double> v_{};
};
struct T1 {
Vec Value;
};
struct T2 {
T2(const T1& t1)
: Value(std::move(t1.Value))
{
}
Vec Value;
};
struct T3 {
T3(T1&& t1)
: Value(std::move(t1.Value))
{
}
Vec Value;
};
int main() {
T1 t1;
T2 t2{ t1 };
T3 t3{ std::move(t1) };
return 0;
}
输出:
copy Vec
move Vec
在荷兰国际集团t1.Value
到Vec
构造函数这两种情况下我std::move
”。
是否有可能在Clang(或其他编译器)中启用某些警告,因此它警告说移动实际上会调用复制构造函数?
(铛已经可以警告性能问题时,例如,我通过std::move(result)
从函数返回一个std::unique_ptr
。)
请注意'的std :: move'实际上并没有移动任何东西。它只是演员。 – NathanOliver
性能问题,当你std ::移动一个'unique_ptr'从函数返回它?你在说什么?这实际上解决了一个或两个指针分配。 – David
@David:是的,但带有命名返回值elision,编译器可以将它变成* nothing *。也就是说,如果他没有不必要地使用'std :: move'。这是一个完全有效的警告。 –