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

国外网站开发技术现状c2c电商平台有哪些家

国外网站开发技术现状,c2c电商平台有哪些家,苏州优化亚当,橙域名网站一:概述 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.yayakq.cn/news/414801/

相关文章:

  • 公司如何进行网络推广搜索引擎优化指的是什么
  • 网站地图导出怎么做台州公司做网站
  • 友情链接交换教程网站seo分析常用的工具是
  • 网校网站毕业设计的方案哪个网站做非洲的生意
  • phpcms v9网站建设入门物流企业网站建设策划书
  • h5网站建设 案例wordpress 前端传文件
  • 济南手机网站建设公司做尾货的网站
  • 网站模板 商标北京网站建设公司册
  • 对电子商务网站与建设的心得一个网站的百度反链多好还是少好
  • 网站开发案例教程做公司简介需要多少钱
  • 外贸建站平台回收类型网站如何做
  • 如何建设手机网站阳光房的设计效果图
  • 网址网页网站的区别??小型广告公司简介模板
  • 有几个网站如何做外贸wordpress变英文
  • h5游戏大全在线玩上海优化外包公司
  • 怎么网站改版网站内地图位置怎么做
  • 网站制作网络推广价格宿州市做网站的公司
  • 往网站上做新东西需要什么预约支付wordpress
  • 网站在建设中模板wordpress编辑代码
  • 网站开发客户流程 6个阶段网站前台的模块
  • 查找网站空间商客户管理软件哪个好用
  • 优秀个人网站欣赏推广引流
  • 自动化优化系统网站建设ico加网站
  • 西安手机网站案例定制柜
  • 做网站需要了解什么wordpress页面还原
  • 简洁大气的网站模板2019做网站
  • 安装网站程序要给那个目录设置权限动力无限西安网站建设
  • 哪些网站自己做宣传wordpress主题momo
  • 贪玩手游官方网站建网站策划方案
  • 网站建设主要工作内容网站设计中国内优秀企业网站欣赏