网站开发现在是热门专业吗淘宝上做网站排名
2026/2/12 2:41:35 网站建设 项目流程
网站开发现在是热门专业吗,淘宝上做网站排名,网站建设源码是什么,代理注册公司排名C20 Pair 实现详解 代码概览 这是一个模仿 std::pair 的现代 C20 实现#xff0c;使用了 concepts、完美转发等特性。 关键点 1. 默认成员初始化器 T1 first{}; T2 second{};为什么用 {}#xff1f;写法Pairint, int p; 的结果T1 first;first 是垃圾值T1 first{};fir…C20 Pair 实现详解代码概览这是一个模仿std::pair的现代 C20 实现使用了 concepts、完美转发等特性。关键点1. 默认成员初始化器T1 first{};T2 second{};为什么用{}写法Pairint, int p;的结果T1 first;first是垃圾值T1 first{};first是0{}确保值初始化基本类型会被初始化为零。2. 默认构造函数的约束constexprPair()requires(std::default_initializableT1std::default_initializableT2)default;作用只有当T1和T2都可以默认构造时Pair才能默认构造。structNoDefault{NoDefault(int){}// 没有默认构造函数};PairNoDefault,intp1;// 编译错误清晰的错误信息PairNoDefault,intp2(NoDefault(1),2);// OK3. 转发构造函数templatetypenameU1,typenameU2constexprPair(U1x,U2y):first(std::forwardU1(x)),second(std::forwardU2(y)){}关键概念万能引用 vs 右值引用templatetypenameTvoidfoo(Tx);// T 在此处推导 → 万能引用templatetypenameTstructBar{voidbaz(Tx);// T 已经固定 → 右值引用};为什么用std::forwardPairCounter,Counterp1(c,c);// c 是左值 → 拷贝PairCounter,Counterp2(std::move(c),Counter(10));// 右值 → 移动std::forward保持参数的值类别左值/右值。4. 转换构造函数的约束templatetypenameU1,typenameU2requires(!std::same_asPairU1,U2,PairT1,T2)constexprPair(constPairU1,U2other)为什么需要这个约束防止与拷贝构造函数冲突Pairint,intp1(1,2);Pairint,intp2(p1);// 应该调用拷贝构造函数不是转换构造函数如果没有约束两个构造函数都匹配可能导致歧义。5. default的含义Pair(constPair)default;意思让编译器生成默认实现。注意 default不等于 “初始化所有成员为零”而是 “做编译器默认会做的事”。structS{intx;S()default;};S s1;// x 是垃圾值默认初始化S s2{};// x 是 0值初始化6.make_pair使用std::decay_ttemplatetypenameT1,typenameT2constexprPairstd::decay_tT1,std::decay_tT2make_pair(T1x,T2y)为什么输入类型std::decay_t结果intintconst intintint[10]int*int(double)int(*)(double)避免存储引用或数组类型inta1;autopmake_pair(a,2);// Pairint, int不是 Pairint, int7. Swap 的 ADL 技巧voidswap(Pairother)noexcept{usingstd::swap;// 引入 std::swap 作为后备swap(first,other.first);// ADL 会找到更好的重载swap(second,other.second);}如果T1有自定义的swap会优先使用它。总结表构造函数用途关键点Pair()默认构造需要约束default_initializablePair(U1, U2)从任意值构造完美转发Pair(const Pair)拷贝 defaultPair(Pair)移动 defaultPair(const PairU1,U2)转换拷贝需要排除相同类型Pair(PairU1,U2)转换移动需要排除相同类型#includeconcepts#includetype_traits#includeutilitytemplatetypenameT1,typenameT2structPair{T1 first{};T2 second{};// ConstructorsconstexprPair()requires(std::default_initializableT1std::default_initializableT2)default;// DefaulttemplatetypenameU1,typenameU2constexprPair(U1x,U2y):first(std::forwardU1(x)),second(std::forwardU2(y)){}// ForwardingPair(constPair)default;// CopyPair(Pair)default;// MovetemplatetypenameU1,typenameU2requires(!std::same_asPairU1,U2,PairT1,T2)constexprPair(constPairU1,U2other):first(other.first),second(other.second){}// Converting copytemplatetypenameU1,typenameU2requires(!std::same_asPairU1,U2,PairT1,T2)constexprPair(PairU1,U2other):first(std::move(other.first)),second(std::move(other.second)){}// Converting move// AssignmentPairoperator(constPairother)default;Pairoperator(Pairother)noexceptdefault;templatetypenameU1,typenameU2requires(!std::same_asPairU1,U2,PairT1,T2)Pairoperator(constPairU1,U2other){firstother.first;secondother.second;return*this;}templatetypenameU1,typenameU2requires(!std::same_asPairU1,U2,PairT1,T2)Pairoperator(PairU1,U2other){firststd::move(other.first);secondstd::move(other.second);return*this;}// Swapvoidswap(Pairother)noexcept{usingstd::swap;swap(first,other.first);swap(second,other.second);}// Comparison (C20)booloperator(constPairother)constdefault;autooperator(constPairother)constdefault;};// Helper functionstemplatetypenameT1,typenameT2constexprPairstd::decay_tT1,std::decay_tT2make_pair(T1x,T2y){returnPairstd::decay_tT1,std::decay_tT2(std::forwardT1(x),std::forwardT2(y));}templatetypenameT1,typenameT2voidswap(PairT1,T2a,PairT1,T2b)noexcept{a.swap(b);}

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询