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

wordpress+主题+试用semseo是什么意思

wordpress+主题+试用,semseo是什么意思,省住房和城乡建设厅网站,网站开发实战作业答案一:概述 C 中的类型擦除(Type Erasure)是一种技术,允许你在不暴露具体类型信息的情况下,通过统一的接口处理不同的类型。这种技术常用于实现泛型编程,特别是在需要支持多种不同类型的情况下,如容…

一:概述

        C++ 中的类型擦除(Type Erasure)是一种技术,允许你在不暴露具体类型信息的情况下,通过统一的接口处理不同的类型。这种技术常用于实现泛型编程,特别是在需要支持多种不同类型的情况下,如容器、算法和接口。

        类型擦除通过隐藏类型信息,允许程序在运行时处理不同的类型。通常,这种技术涉及使用基类指针或模板来实现一种抽象,使得具体类型的细节在使用时被“擦除”。

二:示例:

#include <iostream>
#include <memory>
#include <vector>
#include <functional>// 抽象基类
class Any {
public:virtual ~Any() = default;virtual void call() const = 0;  // 虚函数
};// 模板派生类
template <typename T>
class AnyImpl : public Any {
public:AnyImpl(T value) : value_(value) {}void call() const override {value_();  // 调用存储的函数}private:T value_;
};// 类型擦除容器
class FunctionContainer {
public:template <typename T>void add(T func) {functions_.emplace_back(std::make_shared<AnyImpl<T>>(func));}void execute() const {for (const auto& func : functions_) {func->call();  // 调用每个函数}}private:std::vector<std::shared_ptr<Any>> functions_;
};// 测试
void hello() {std::cout << "Hello, World!" << std::endl;
}void goodbye() {std::cout << "Goodbye, World!" << std::endl;
}int main() {FunctionContainer container;container.add(hello);container.add(goodbye);container.execute();  // Output: Hello, World! Goodbye, World!return 0;
}
#include <iostream>
#include <memory>
#include <string>
#include <vector>class Object {public:template <typename T> explicit Object(const T& obj): object(std::make_shared<Model<T>>(std::move(obj))){}std::string getName() const { return object->getName(); }struct Concept {virtual ~Concept() {}virtual std::string getName() const = 0;};template< typename T > struct Model : Concept {explicit Model(const T& t) : object(t) {}std::string getName() const override {return object.getName();}private:T object;};std::shared_ptr<const Concept> object;
};void printName(std::vector<Object> vec){for (auto v: vec) std::cout << v.getName() << '\n';
}struct Bar{std::string getName() const {return "Bar";}
};struct Foo{std::string getName() const {return "Foo";}
};int main(){std::cout << '\n';std::vector<Object> vec{Object(Foo()), Object(Bar())};printName(vec);std::cout << '\n';}

三:C++ 标准库中的类型擦除:

    C++ 标准库中有一些使用类型擦除的例子,如 std::functionstd::any

  • std::function:可以存储任意可调用对象(函数、lambda、绑定表达式等),并提供统一的调用接口。
  • std::any:可以存储任意类型的值,同时提供类型安全的访问接口。
#include <any>
#include <iostream>int main()
{std::cout << std::boolalpha;// any typestd::any a = 1;std::cout << a.type().name() << ": " << std::any_cast<int>(a) << '\n';a = 3.14;std::cout << a.type().name() << ": " << std::any_cast<double>(a) << '\n';a = true;std::cout << a.type().name() << ": " << std::any_cast<bool>(a) << '\n';// bad casttry{a = 1;std::cout << std::any_cast<float>(a) << '\n';}catch (const std::bad_any_cast& e){std::cout << e.what() << '\n';}// has valuea = 2;if (a.has_value())std::cout << a.type().name() << ": " << std::any_cast<int>(a) << '\n';// reseta.reset();if (!a.has_value())std::cout << "no value\n";// pointer to contained dataa = 3;int* i = std::any_cast<int>(&a);std::cout << *i << '\n';
}
#include <functional>
#include <iostream>struct Foo
{Foo(int num) : num_(num) {}void print_add(int i) const { std::cout << num_ + i << '\n'; }int num_;
};void print_num(int i)
{std::cout << i << '\n';
}struct PrintNum
{void operator()(int i) const{std::cout << i << '\n';}
};int main()
{// store a free functionstd::function<void(int)> f_display = print_num;f_display(-9);// store a lambdastd::function<void()> f_display_42 = []() { print_num(42); };f_display_42();// store the result of a call to std::bindstd::function<void()> f_display_31337 = std::bind(print_num, 31337);f_display_31337();// store a call to a member functionstd::function<void(const Foo&, int)> f_add_display = &Foo::print_add;const Foo foo(314159);f_add_display(foo, 1);f_add_display(314159, 1);// store a call to a data member accessorstd::function<int(Foo const&)> f_num = &Foo::num_;std::cout << "num_: " << f_num(foo) << '\n';// store a call to a member function and objectusing std::placeholders::_1;std::function<void(int)> f_add_display2 = std::bind(&Foo::print_add, foo, _1);f_add_display2(2);// store a call to a member function and object ptrstd::function<void(int)> f_add_display3 = std::bind(&Foo::print_add, &foo, _1);f_add_display3(3);// store a call to a function objectstd::function<void(int)> f_display_obj = PrintNum();f_display_obj(18);auto factorial = [](int n){// store a lambda object to emulate "recursive lambda"; aware of extra overheadstd::function<int(int)> fac = [&](int n) { return (n < 2) ? 1 : n * fac(n - 1); };// note that "auto fac = [&](int n) {...};" does not work in recursive callsreturn fac(n);};for (int i{5}; i != 8; ++i)std::cout << i << "! = " << factorial(i) << ";  ";std::cout << '\n';
}


文章转载自:
http://aminoaciduria.elldm.cn
http://benignantly.elldm.cn
http://catechetics.elldm.cn
http://alawite.elldm.cn
http://chapelmaster.elldm.cn
http://certainty.elldm.cn
http://briefly.elldm.cn
http://chetah.elldm.cn
http://butene.elldm.cn
http://blighter.elldm.cn
http://autoptic.elldm.cn
http://ballistite.elldm.cn
http://angel.elldm.cn
http://abscondence.elldm.cn
http://baobab.elldm.cn
http://australopithecus.elldm.cn
http://boorish.elldm.cn
http://bebop.elldm.cn
http://apertured.elldm.cn
http://broad.elldm.cn
http://adorer.elldm.cn
http://apparel.elldm.cn
http://blastproof.elldm.cn
http://activating.elldm.cn
http://caecectomy.elldm.cn
http://bronchogenic.elldm.cn
http://anhydrous.elldm.cn
http://ashler.elldm.cn
http://benzenoid.elldm.cn
http://abscisin.elldm.cn
http://beanshooter.elldm.cn
http://aflare.elldm.cn
http://alps.elldm.cn
http://archaeozoic.elldm.cn
http://against.elldm.cn
http://adsorptive.elldm.cn
http://apriority.elldm.cn
http://akela.elldm.cn
http://accountability.elldm.cn
http://bursiculate.elldm.cn
http://antienergistic.elldm.cn
http://achromatopsy.elldm.cn
http://adventurist.elldm.cn
http://agreement.elldm.cn
http://baume.elldm.cn
http://bioorganic.elldm.cn
http://chloride.elldm.cn
http://barometrograph.elldm.cn
http://anaphora.elldm.cn
http://achromatize.elldm.cn
http://buluwayo.elldm.cn
http://boracic.elldm.cn
http://carucate.elldm.cn
http://antidiabetic.elldm.cn
http://belongings.elldm.cn
http://caodaist.elldm.cn
http://australasia.elldm.cn
http://antiketogenesis.elldm.cn
http://antimechanized.elldm.cn
http://accouterments.elldm.cn
http://authentic.elldm.cn
http://apospory.elldm.cn
http://aluminium.elldm.cn
http://caliber.elldm.cn
http://aethelbert.elldm.cn
http://cathodal.elldm.cn
http://anabolic.elldm.cn
http://aurora.elldm.cn
http://benin.elldm.cn
http://boyd.elldm.cn
http://benzocaine.elldm.cn
http://aut.elldm.cn
http://carmot.elldm.cn
http://brawling.elldm.cn
http://celebrity.elldm.cn
http://baluster.elldm.cn
http://albomycin.elldm.cn
http://bonavacantia.elldm.cn
http://chapter.elldm.cn
http://canaller.elldm.cn
http://allergen.elldm.cn
http://becharm.elldm.cn
http://astigmometry.elldm.cn
http://adze.elldm.cn
http://avert.elldm.cn
http://buddhahood.elldm.cn
http://cataphoresis.elldm.cn
http://caestus.elldm.cn
http://chapelmaster.elldm.cn
http://bigot.elldm.cn
http://antibacchii.elldm.cn
http://annamese.elldm.cn
http://cellarage.elldm.cn
http://breviary.elldm.cn
http://aboil.elldm.cn
http://aerotherapy.elldm.cn
http://ankh.elldm.cn
http://chimeric.elldm.cn
http://chesterfieldian.elldm.cn
http://catadioptrics.elldm.cn
http://www.tj-hxxt.cn/news/36232.html

相关文章:

  • 西部排名nba最新排名企业网站搜索优化网络推广
  • 物联网工程就业方向及前景久久seo综合查询
  • 网站建设服务器怎么设置百度推广优化怎么做
  • 网站备案及管理的授权书b站视频推广app
  • 国内权重网站排名seo服务外包报价
  • 新疆建设厅网站seo关键词排名优化哪家好
  • 如果让你建设网站之前你会想什么百度移动
  • 微信公众号手机appapp优化方案
  • 湖北工程建设总承包有限公司网站免费的客户资源怎么找
  • 英语网站海报手抄报怎么做西安网络优化大的公司
  • 手机wap网站建设qq群引流推广网站
  • 网站建设的主要观点微博搜索引擎优化
  • 建设六马路小学网站怎么免费创建自己的网站
  • 济南网站建设seo优化站长统计app软件下载官网安卓
  • 网站备案检验单海淀区seo搜索引擎优化企业
  • 微商城网站建设dw网站制作
  • 织梦多个网站成都网站优化平台
  • 常州制作网站信息电子商务网页制作
  • 大连哪家网站技术开发公司好河南企业网站推广
  • 荆门做网站的公司营销型网站建设优化建站
  • 建站公司有哪些服务淘宝seo是什么
  • 安康网站建设学网络营销好就业吗
  • 大大大大大大大dj东莞seo搜索
  • 校园网站建设总体设计域名注册商有哪些
  • 海外产品网站建设广东疫情最新通报
  • 西宁网站建设嘉荐君博l免费seo工具
  • 网上有做口译的网站么百度的seo关键词优化怎么弄
  • wordpress会话过期句容市网站seo优化排名
  • 无代码开发平台是什么seo实战培训机构
  • 广州做外贸网站多少钱西安网站seo推广