brSmoothWeights:Maya 皮肤权重平滑,关节过渡一次处理到位
2026/8/22 19:34:12
在C++11之前,对象资源的转移通常需要通过拷贝来完成,这可能导致不必要的性能开销。考虑以下场景:
std::vector<std::string>createLargeVector(){std::vector<std::string>v;// 添加大量数据for(inti=0;i<10000;i++){v.push_back("some long string data...");}returnv;// C++11前:可能发生拷贝,性能低下}移动语义的出现解决了这一问题,允许资源所有权的转移而非拷贝,std::move()正是实现这一机制的关键工具。
std::move()定义在<utility>头文件中,实际上并不移动任何东西。它的核心作用是将左值转换为右值引用,从而允许调用移动构造函数或移动赋值运算符。
template<typenameT>typenamestd::remove_reference<T>::type&&move(T&&arg)noexcept{returnstatic_cast<typenamestd::remove_reference<T>::type&&>(arg);}std::move()只是类型转换,真正的移动发生在移动构造函数/赋值运算符中classBuffer{private:char*data;size_t size;public:// 移动构造函数Buffer(Buffer&&other)noexcept:data(other.data),size(other.size){other.data=nullptr;other.size=0;}// 移动赋值运算符Buffer&operator=(Buffer&&other)noexcept{if(this!=&other){delete[]data;data=other.data;size=other.size;other.data=nullptr;other.size=0;}return*this;}};BuffercreateBuffer(){Bufferbuf(1024);// ... 填充数据returnstd::move(buf);// 触发移动而非拷贝}std::vector<std::string>processStrings(std::vector<std::string>&strings){std::vector<std::string>result;for(auto&str:strings){if(shouldProcess(str)){// 移动而非拷贝,提高性能result.push_back(std::move(str));}}returnresult;}classResourceHolder{private:std::unique_ptr<Resource>resource;public:voidsetResource(std::unique_ptr<Resource>newResource){// 必须使用移动,因为unique_ptr不可拷贝resource=std::move(newResource);}};classMyString{private:char*data;size_t length;public:// 移动构造函数MyString(MyString&&other)noexcept:data(other.data),length(other.length){// 转移资源所有权other.data=nullptr;other.length=0;}// 移动赋值运算符MyString&operator=(MyString&&other)noexcept{if(this!=&other){delete[]data;// 释放当前资源// 转移资源data=other.data;length=other.length;// 置空源对象other.data=nullptr;other.length=0;}return*this;}};// 错误示例:不必要的移动std::stringgetName(){std::string name="John";returnstd::move(name);// 错误!NRVO可能被抑制}// 正确:让编译器优化std::stringgetName(){std::string name="John";returnname;// 编译器可能使用NRVO}std::string str1="Hello";std::string str2=std::move(str1);// str1现在处于有效但未指定状态// 不应该再依赖str1的内容// 但可以重新赋值使用str1="New Content";// 这是安全的// 不必要的移动autovec=std::move(std::vector<int>{1,2,3});// 正确:直接使用autovec=std::vector<int>{1,2,3};conststd::string constStr="Hello";autostr=std::move(constStr);// 不会移动!会调用拷贝构造函数std::move()常与完美转发结合使用:
template<typenameT>voidprocess(T&&arg){// 如果arg是右值,则移动;如果是左值,则保持store(std::forward<T>(arg));}template<typenameT>voidwrapper(T&&arg){// 使用std::forward保持值类别process(std::forward<T>(arg));}#include<chrono>#include<vector>voidtestPerformance(){constintsize=1000000;// 测试拷贝autostart=std::chrono::high_resolution_clock::now();std::vector<int>v1(size,42);std::vector<int>v2=v1;// 拷贝autoend=std::chrono::high_resolution_clock::now();autocopyTime=std::chrono::duration_cast<std::chrono::microseconds>(end-start);// 测试移动start=std::chrono::high_resolution_clock::now();std::vector<int>v3(size,42);std::vector<int>v4=std::move(v3);// 移动end=std::chrono::high_resolution_clock::now();automoveTime=std::chrono::duration_cast<std::chrono::microseconds>(end-start);std::cout<<"Copy time: "<<copyTime.count()<<"μs\n";std::cout<<"Move time: "<<moveTime.count()<<"μs\n";}std::move()是类型转换,不是移动操作std::move(),以免抑制RVO/NRVO希望这篇详解能帮助你更好地理解和应用C++中的移动语义!