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

小程序开发 网站建设怎么查找关键词排名

小程序开发 网站建设,怎么查找关键词排名,微企点建好网站后要怎么做,《学做网站论坛》视频下载一:概述 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://www.mmbaike.com/news/50879.html

相关文章:

  • php开发网站建设线上广告推广平台
  • 用DW做的网站怎么弄成链接惠州百度seo在哪
  • wordpress 站点地址无锡百度快照优化排名
  • 用jquery做网站搭建网站多少钱
  • 石化建设分会网站怎么给公司做网站
  • 网站建设管理分工的说明网站seo 优化
  • 郑州最好的男科医院是什么医院宁波专业seo服务
  • 嘉兴做网站seo的正规排名网站推广公司
  • 建立网站的用处网络营销的特点是什么?
  • bp链接生成器网站外贸电商平台哪个网站最好
  • 济南网站建设开发公司哪家好班级优化大师
  • 宜昌网站设计制作公司排行榜
  • 杭州做网站的好公司有哪些福州seo经理招聘
  • 黄岩地区做环评立项在哪个网站广州市运营推广公司
  • 做私活一个网站大概多少钱个人主页网页设计模板
  • 怎么安装php网站公众号怎么做文章推广
  • wordpress著名网站东莞seo培训
  • 建筑模型网站有哪些网络推广平台软件
  • ftp上传网站全教程it培训班大概需要多少钱
  • 做网站用什么语言自助建站系统源码
  • 人力社保网站建设的意义app推广接单平台有哪些
  • 手机网站建设的目的百度的广告怎么免费发布
  • 怎么做公益网站网推资源渠道
  • 轻创优选地推appseo初学教程
  • 网站上传好了如何做定向最新足球新闻头条
  • 做网站和网页有什么区别百度快照
  • 企石镇仿做网站百度业务员联系电话
  • 上海seo什么是seo推广
  • 现在网站都是拿什么软件做的seo咨询河北
  • wordpress升级主题总是失败seo需要付费吗