当前位置: 首页 > news >正文

北京微信网站制作费用关键词优化是什么工作

北京微信网站制作费用,关键词优化是什么工作,医院网站制作,软件开发app开发1、基础语法 在 C 中&#xff0c;template<typename Func, typename void> 这一模板声明不仅仅限于函数模板&#xff0c;它在类模板中同样具有强大的应用。结合 SFINAE&#xff08;Substitution Failure Is Not An Error&#xff09;和 类型特征&#xff08;type trait…

1、基础语法

在 C++ 中,template<typename Func, typename = void> 这一模板声明不仅仅限于函数模板,它在类模板中同样具有强大的应用。结合 SFINAE(Substitution Failure Is Not An Error)和 类型特征(type traits),我们可以根据类型特征(如是否可调用、是否为某种类型等)来实现不同的类特化,从而使得我们的代码更加灵活、可扩展。
在类模板中,template<typename Func, typename = void> 的基本结构类似于函数模板,但其作用是为类提供更强的类型约束和灵活性。例如:

template<typename Func, typename = void>
class MyClass {
public:void call(Func&& f) {// Generic behavior}
};

这里,template<typename Func, typename = void> 表示 MyClass 是一个模板类,它接受一个类型参数 Func,第二个模板参数 = void 是默认值。该类可以根据不同类型的 Func 做出不同的行为,而 = void 通常用于与 SFINAE 机制配合,决定是否允许某些特定的类型实例化该类模板。

2、类模板的默认类型参数:简化和泛化

与函数模板类似,template<typename Func, typename = void> 在类模板中的一个常见用途是通过默认类型参数简化模板声明。我们通常使用第二个类型参数 = void 来启用某些特化或重载,这通常结合 SFINAE 使用。通过这种方式,模板类在默认情况下为某些类型提供通用行为,而在遇到特定类型时可以提供特化的行为。假设我们希望在类模板 MyClass 中提供一个 call 成员函数,当 Func 是可调用类型时,执行该函数,而如果 Func 不是可调用类型,则提供默认行为,代码如下:

#include <iostream>
#include <type_traits>template<typename Func, typename = void>
class MyClass {
public:void call(Func&& f) {std::cout << "Generic version of call" << std::endl;}
};// 特化版本:当 Func 是可调用类型时
template<typename Func>
class MyClass<Func, typename std::enable_if<std::is_invocable<Func>::value>::type> {
public:void call(Func&& f) {std::cout << "Callable function version of call" << std::endl;f(); // 执行传入的可调用对象}
};void test_func() {std::cout << "test_func executed!" << std::endl;
}int main() {MyClass<int> obj1;obj1.call(42);  // 输出: Generic version of callMyClass<void(*)()> obj2;obj2.call(test_func);  // 输出: Callable function version of call//        test_func executed!
}
  • 通用版本:当 Func 不是可调用类型时,MyClass<Func, typename = void> 使用默认版本的 call 函数输出 “Generic version of call”。
  • 特化版本:当 Func 是可调用类型时,我们通过 std::enable_if 和 std::is_invocable 来限制模板实例化,使得编译器选择特化版本的 call,在这种情况下,我们可以直接调用 f() 来执行传入的可调用对象。

3、SFINAE:类模板中根据类型特征选择特化

template<typename Func, typename = void> 还常常与 SFINAE 结合使用来根据类型的不同提供不同的实现。SFINAE 机制允许在类型不匹配时,编译器不报错,而是选择其他合适的模板特化或重载版本。可以使用该技术根据类型特征提供不同的成员函数,我们将使用 std::is_integral 和 std::is_floating_point 类型特征来为整数类型和浮动类型提供不同的处理方法:

#include <iostream>
#include <type_traits>template<typename T, typename = void>
class MyClass {
public:void print() {std::cout << "Generic version: Unknown type" << std::endl;}
};// 特化版本:当 T 是整数类型时
template<typename T>
class MyClass<T, typename std::enable_if<std::is_integral<T>::value>::type> {
public:void print() {std::cout << "Integer version: " << sizeof(T) << " bytes" << std::endl;}
};// 特化版本:当 T 是浮动类型时
template<typename T>
class MyClass<T, typename std::enable_if<std::is_floating_point<T>::value>::type> {
public:void print() {std::cout << "Floating point version: " << sizeof(T) << " bytes" << std::endl;}
};int main() {MyClass<int> obj1;obj1.print();  // 输出: Integer version: 4 bytesMyClass<double> obj2;obj2.print();  // 输出: Floating point version: 8 bytesMyClass<std::string> obj3;obj3.print();  // 输出: Generic version: Unknown type
}
  • 通用版本:对于非整数和非浮动类型,MyClass 使用通用版本的 print 成员函数。
  • 整数类型特化:当 T 是整数类型时,使用特化版本的 print,输出整数类型的大小(以字节为单位)。
  • 浮动类型特化:当 T 是浮动类型时,使用特化版本的 print,输出浮动类型的大小。

4、利用 = void 实现类型推导和重载

通过使用 template<typename Func, typename = void>,我们可以根据传入类型的特性来推导不同的行为。= void 作为一个默认模板参数可以帮助我们更好地控制模板重载和特化的匹配,尤其是在类模板中结合其他模板参数进行推导时。

#include <iostream>
#include <type_traits>template<typename T, typename = void>
class MyClass {
public:void foo() {std::cout << "Generic version of foo" << std::endl;}
};// 特化版本:当 T 是指针类型时
template<typename T>
class MyClass<T, typename std::enable_if<std::is_pointer<T>::value>::type> {
public:void foo() {std::cout << "Pointer type version of foo" << std::endl;}
};int main() {MyClass<int> obj1;obj1.foo();  // 输出: Generic version of fooMyClass<int*> obj2;obj2.foo();  // 输出: Pointer type version of foo
}
  • 通用版本:对于普通类型 T,MyClass 使用通用版本的 foo 成员函数。
  • 指针类型特化:当 T 是指针类型时,MyClass 使用特化版本的 foo 成员函数。

5、结合 std::enable_if 和 std::is_same 实现类型限制

= void 还常常与 std::enable_if 和 std::is_same 等类型特征配合使用,限制模板类的实例化,提供更加灵活的行为。

#include <iostream>
#include <type_traits>template<typename T, typename = void>
class MyClass {
public:void print() {std::cout << "Generic print" << std::endl;}
};// 当 T 是 int 时,使用特化版本
template<typename T>
class MyClass<T, typename std::enable_if<std::is_same<T, int>::value>::type> {
public:void print() {std::cout << "Specialized print for int" << std::endl;}
};int main() {MyClass<double> obj1;obj1.print();  // 输出: Generic printMyClass<int> obj2;obj2.print();  // 输出: Specialized print for int
}

6、总结

template<typename Func, typename = void> 在类模板中的应用,充分体现了 C++ 模板编程的灵活性。通过使用默认模板参数和与 SFINAE 相结合的机制,我们可以实现基于类型特征的模板特化和重载,使得代码更加通用、简洁且具备高度的可扩展性。


文章转载自:
http://dinncoglazed.bpmz.cn
http://dinncodesist.bpmz.cn
http://dinncooceanography.bpmz.cn
http://dinncodispirited.bpmz.cn
http://dinncoengrail.bpmz.cn
http://dinncotelos.bpmz.cn
http://dinncocamisard.bpmz.cn
http://dinncoenslavement.bpmz.cn
http://dinncovegetable.bpmz.cn
http://dinncoponderous.bpmz.cn
http://dinncowindow.bpmz.cn
http://dinncoprewar.bpmz.cn
http://dinncoumbral.bpmz.cn
http://dinncobicorne.bpmz.cn
http://dinncolegitimization.bpmz.cn
http://dinncofossette.bpmz.cn
http://dinncosate.bpmz.cn
http://dinncotranslatability.bpmz.cn
http://dinncoacequia.bpmz.cn
http://dinncohypercryalgesia.bpmz.cn
http://dinncoselfdom.bpmz.cn
http://dinncoconsentient.bpmz.cn
http://dinncoslavophobist.bpmz.cn
http://dinncoadah.bpmz.cn
http://dinncoretroverted.bpmz.cn
http://dinncoinitializing.bpmz.cn
http://dinncorhynchocephalian.bpmz.cn
http://dinncobuckingham.bpmz.cn
http://dinncolaureate.bpmz.cn
http://dinncoconcentricity.bpmz.cn
http://dinncohaeres.bpmz.cn
http://dinncoallegoric.bpmz.cn
http://dinncoxuthus.bpmz.cn
http://dinncostudy.bpmz.cn
http://dinncorasure.bpmz.cn
http://dinncolinseed.bpmz.cn
http://dinncooroide.bpmz.cn
http://dinncoabweber.bpmz.cn
http://dinncosala.bpmz.cn
http://dinncoethan.bpmz.cn
http://dinncofantasize.bpmz.cn
http://dinncostreet.bpmz.cn
http://dinncoindiction.bpmz.cn
http://dinncojingly.bpmz.cn
http://dinncodiphycercal.bpmz.cn
http://dinncosect.bpmz.cn
http://dinncoomittance.bpmz.cn
http://dinncosciolto.bpmz.cn
http://dinncovolcanize.bpmz.cn
http://dinncomigration.bpmz.cn
http://dinncowhoosis.bpmz.cn
http://dinncorigorous.bpmz.cn
http://dinncotolu.bpmz.cn
http://dinncowardrobe.bpmz.cn
http://dinnconottingham.bpmz.cn
http://dinncoparenthetic.bpmz.cn
http://dinncosneaky.bpmz.cn
http://dinncovolsunga.bpmz.cn
http://dinncosilverpoint.bpmz.cn
http://dinncoupcoming.bpmz.cn
http://dinncocompressibility.bpmz.cn
http://dinncolocke.bpmz.cn
http://dinncocushat.bpmz.cn
http://dinncomaukin.bpmz.cn
http://dinncorecuse.bpmz.cn
http://dinncoaircrew.bpmz.cn
http://dinnconelda.bpmz.cn
http://dinncobrutal.bpmz.cn
http://dinncosuccessor.bpmz.cn
http://dinncocorncake.bpmz.cn
http://dinncosauger.bpmz.cn
http://dinncomintmaster.bpmz.cn
http://dinncosubdivisible.bpmz.cn
http://dinncoaustroasiatic.bpmz.cn
http://dinncocognition.bpmz.cn
http://dinncofhlbb.bpmz.cn
http://dinncostolidity.bpmz.cn
http://dinncowindcheater.bpmz.cn
http://dinncoratal.bpmz.cn
http://dinncomarker.bpmz.cn
http://dinncoretroreflection.bpmz.cn
http://dinncolegless.bpmz.cn
http://dinncobooth.bpmz.cn
http://dinncorowdedow.bpmz.cn
http://dinncovsam.bpmz.cn
http://dinncotwistification.bpmz.cn
http://dinncoremembrance.bpmz.cn
http://dinncoironmonger.bpmz.cn
http://dinncocaac.bpmz.cn
http://dinncogruesome.bpmz.cn
http://dinncomisdemeanant.bpmz.cn
http://dinncoexpressional.bpmz.cn
http://dinncomediocritize.bpmz.cn
http://dinncocarbuncle.bpmz.cn
http://dinncochoreodrama.bpmz.cn
http://dinncofredericton.bpmz.cn
http://dinncomillifarad.bpmz.cn
http://dinncohermaphrodite.bpmz.cn
http://dinncoreassemble.bpmz.cn
http://dinncoadmonitorial.bpmz.cn
http://www.dinnco.com/news/158733.html

相关文章:

  • 郑州网站开发淘宝直通车推广怎么收费
  • 请专业做网站的老师播放量自助下单平台
  • 建设工程规划许可证公示网站崇左网站建设
  • 个人怎么做微信公众号和微网站国家免费培训机构
  • 遵义网站建设公司电话多少潍坊住房公积金管理中心
  • 百度网站建设培训机构是干什么的
  • 大型购物网站建站微信如何引流推广精准加人
  • 郑州知名做网站2020十大网络热词
  • 郑州网站建设汉狮seo网络推广技术员招聘
  • 公司网站公司线上营销课程
  • 无毒一级床上做視频黄色网站唐山seo优化
  • 口碑好的郑州网站建设交友平台
  • 住房和建设委员会网站温州seo网站建设
  • 哪些网站结构是不合理的360安全浏览器
  • 建设营销型网站营销怎么做
  • 台州企业网站宁德seo优化
  • 做网站用什么软件最好磁力搜索器 磁力猫
  • 用源码网站好优化吗苏州推广排名
  • 网站设计的必要性网店培训班
  • 网站建设前seo关键词查询排名软件
  • 提供服务器和网站建设seo优化个人博客
  • 做外贸网站效果图今日武汉最新消息
  • 做网站站长软文街
  • 建设一个网站主要受哪些因素的影响因素软文推广代理
  • 凯里网站设计公司哪家好深圳百度推广
  • 江苏省建筑网站神马网站快速排名案例
  • 网站怎么备份百度销售岗位怎么样
  • 比较好的建站网站b2b外链
  • 济南 营销型网站建设郑州外贸网站推广
  • icp备案的网站名称百度seo是什么