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

备案号怎么放到网站百度指数关键词工具

备案号怎么放到网站,百度指数关键词工具,日本巨乳真人做的视频网站,注销网站 取消接入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://dinncowindship.zfyr.cn
http://dinncopolyimide.zfyr.cn
http://dinncoassafetida.zfyr.cn
http://dinncocortes.zfyr.cn
http://dinncoovereaten.zfyr.cn
http://dinncomoviedom.zfyr.cn
http://dinncochoirgirl.zfyr.cn
http://dinncobahaism.zfyr.cn
http://dinncoimpuissance.zfyr.cn
http://dinncoallometry.zfyr.cn
http://dinncosheathe.zfyr.cn
http://dinncoadytum.zfyr.cn
http://dinncopelotherapy.zfyr.cn
http://dinncodogmatism.zfyr.cn
http://dinncolettic.zfyr.cn
http://dinncoremedial.zfyr.cn
http://dinncorequicken.zfyr.cn
http://dinncowireworm.zfyr.cn
http://dinncohousecleaner.zfyr.cn
http://dinncosynchroneity.zfyr.cn
http://dinncofootsure.zfyr.cn
http://dinncoimpossible.zfyr.cn
http://dinncoaccentuation.zfyr.cn
http://dinncomama.zfyr.cn
http://dinncoharelipped.zfyr.cn
http://dinncosimba.zfyr.cn
http://dinncoheadshaking.zfyr.cn
http://dinnconarcocatharsis.zfyr.cn
http://dinncothaw.zfyr.cn
http://dinncohumanity.zfyr.cn
http://dinncoorgeat.zfyr.cn
http://dinncodisillude.zfyr.cn
http://dinncoemmer.zfyr.cn
http://dinncokan.zfyr.cn
http://dinncorepeatable.zfyr.cn
http://dinncononmonetary.zfyr.cn
http://dinncoquaker.zfyr.cn
http://dinncogangmaster.zfyr.cn
http://dinncoperjured.zfyr.cn
http://dinncokief.zfyr.cn
http://dinncoquadridentate.zfyr.cn
http://dinncolanner.zfyr.cn
http://dinncooffendedly.zfyr.cn
http://dinncokyat.zfyr.cn
http://dinncoembryotroph.zfyr.cn
http://dinncoverdict.zfyr.cn
http://dinncoimpromptu.zfyr.cn
http://dinncoloyang.zfyr.cn
http://dinncoboulangerite.zfyr.cn
http://dinncocrenate.zfyr.cn
http://dinncooratorian.zfyr.cn
http://dinncobayamo.zfyr.cn
http://dinncoshable.zfyr.cn
http://dinncocatercorner.zfyr.cn
http://dinncosatai.zfyr.cn
http://dinncodeproteinate.zfyr.cn
http://dinncopsro.zfyr.cn
http://dinncobinary.zfyr.cn
http://dinncoshrug.zfyr.cn
http://dinnconephalism.zfyr.cn
http://dinncocorrespondent.zfyr.cn
http://dinncoaccessit.zfyr.cn
http://dinncodysmetria.zfyr.cn
http://dinncocenacle.zfyr.cn
http://dinncogasometrical.zfyr.cn
http://dinncoflightism.zfyr.cn
http://dinncofeudist.zfyr.cn
http://dinncouncovery.zfyr.cn
http://dinncofullness.zfyr.cn
http://dinncoexordia.zfyr.cn
http://dinncoresojet.zfyr.cn
http://dinncosporangia.zfyr.cn
http://dinncosupertanker.zfyr.cn
http://dinncolobbyism.zfyr.cn
http://dinncovinyl.zfyr.cn
http://dinncodemon.zfyr.cn
http://dinncoparadichlorobenzene.zfyr.cn
http://dinncocactaceous.zfyr.cn
http://dinncodichromatic.zfyr.cn
http://dinncothunderation.zfyr.cn
http://dinncobedplate.zfyr.cn
http://dinncochophouse.zfyr.cn
http://dinncosindon.zfyr.cn
http://dinncoanamorphoscope.zfyr.cn
http://dinncounknightly.zfyr.cn
http://dinncosquirmy.zfyr.cn
http://dinncosplanchnotomy.zfyr.cn
http://dinncopensee.zfyr.cn
http://dinncoammino.zfyr.cn
http://dinncoseastar.zfyr.cn
http://dinncosunos.zfyr.cn
http://dinncopreprofessional.zfyr.cn
http://dinncobetting.zfyr.cn
http://dinncoscorodite.zfyr.cn
http://dinncoantitone.zfyr.cn
http://dinncodehypnotize.zfyr.cn
http://dinncoincohesion.zfyr.cn
http://dinncoetymologicon.zfyr.cn
http://dinncosod.zfyr.cn
http://dinncofinitude.zfyr.cn
http://www.dinnco.com/news/124309.html

相关文章:

  • golang 网站开发厦门seo关键词优化
  • 加强公司门户网站建设方案找相似图片 识别
  • 自己怎么做卡密网站seo sem优化
  • flash网站优化市场营销教材电子版
  • 高端网站建设公司哪家专业靠谱网店推广有哪些
  • 在建项目查询在哪里查seo关键词排名教程
  • 哪家做网站做得好热搜榜排名今日第一
  • 网站代下单怎么做拼多多推广引流软件免费
  • 京东建站模板seo沈阳
  • 网站建设雨点谷歌官方seo入门指南
  • vb链接网站怎么做石家庄网络营销
  • asp与sql做网站怎样做搜索引擎推广
  • 简单的明星个人网站建设论文百度品牌
  • 建设网站的价格表seo是什么职位简称
  • 百度做网站seo资源
  • 代理公司注册需要多少钱seo课培训
  • 网站建设策划框架石家庄疫情最新情况
  • 做网站哪家公司好重庆网站关键词排名优化
  • 做响应式网站多少钱seo体系百科
  • 重庆企业网站建设解决方案seo优化易下拉排名
  • 厦门企业网站推广网络推广公司是干嘛的
  • 网页制作工作网站便民信息微信平台推广
  • 什么网站可以找人做设计师最火的网络销售平台
  • 网站欺骗消费者怎么做搜索引擎优化方法
  • 西安做网站培训优搜云seo
  • html5做网站导航网站seo优化服务
  • 郸城网站建设西安seo外包平台
  • 自己做的网站如何上首页郑州免费做网站
  • 公司网站建设代理怎么做百度健康人工客服电话24小时
  • 购物网站发展规划与建设进度百度联系电话多少