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

深圳网站建设学习爱站之家

深圳网站建设学习,爱站之家,wordpress壁纸主题下载失败,企业做网站做什么科目类sdk:any提供类型安全的容器来存储任何类型的单个值。通俗地说,std::any是一个容器,可以在其中存储任何值(或用户数据),而无需担心类型安全。void*的功能有限,仅存储指针类型,被视为不安全模式。std::any可以被视为vo…

      类sdk:any提供类型安全的容器来存储任何类型的单个值。通俗地说,std::any是一个容器,可以在其中存储任何值(或用户数据),而无需担心类型安全。void*的功能有限,仅存储指针类型,被视为不安全模式。std::any可以被视为void*的类型安全替代品。
      std::any初始化:拷贝初始化;使用参数化构造函数;大括号初始值设定(brace initializer);使用赋值运算符;使用std::make_any。
      必须使用std::any_cast<type>(any_var)函数将any_var值转换为原始类型。std::any_cast<type>(any_var)函数有一些重载,它可以返回副本、引用或指针,具体取决于调用方式。如果存储值的类型不是尝试转换的类型,则编译器将抛出std::bad_any_cast异常或返回nullptr转换期间的类型必须与原始类型完全相同

int test_any_init()
{// copy initialisationstd::any value = 66; // 推荐: std::any value = std::make_any<int>(66); // std::make_any:类型安全、异常安全std::cout << "value: " << std::any_cast<int>(value) << "\n"; // value: 66// assignment operatorvalue = "China";std::cout << "value: " << std::any_cast<const char*>(value) << "\n"; // value: China// parametrized constructorstd::any value2(88.);std::cout << "value2: " << std::any_cast<double>(value2) << "\n"; // value2: 88// brace initializertry {std::any value3{ "China" };std::cout << "value3: " << std::any_cast<std::string>(value3) << "\n"; // std::any_cast<const char*>(value3)} catch (std::bad_any_cast& e) {std::cout << "Error: " << e.what() << "\n"; // value3: Error: Bad any_cast}auto value4 = std::make_any<std::string>("Beijing");std::cout << "value4: " << std::any_cast<std::string&>(value4) << "\n"; // value4: Beijing 推荐转换为引用类型来避免创建临时对象std::string str = "Tianjin";std::any value5 = std::move(str);std::cout << "value5: " << std::any_cast<std::string&>(value5) << "\n"; // value5: Tianjinreturn 0;
}

      std::any的成员函数
      (1).emplace:改变存储的对象,直接构造新对象;
      (2).reset:通过调用对象的析构函数来销毁存储的对象;
      (3).has_value:用于检查对象是否存储值;
      (4).type:返回一个type_info结构体,可用于获取存储对象的属性,如存储值的类型ID

int test_any_member_functions()
{// emplace, typestd::any value = 6;std::cout << "value: " << std::any_cast<int>(value) << "\n"; // value: 6std::cout << "type name: " << value.type().name() << "\n"; // type name: int(windows), i(linux)auto& tmp = std::any_cast<int&>(value); // 引用tmp = 8;std::cout << "value: " << std::any_cast<int>(value) << "\n"; // value: 8std::any_cast<int&>(value) = 10;std::cout << "value: " << std::any_cast<int>(value) << "\n"; // value: 10auto ptr = std::any_cast<int>(&value); // 指针std::cout << "value: " << *ptr << "\n"; // value: 10// 避免抛异常auto ptr2 = std::any_cast<std::string>(&value);if (ptr2 == nullptr)std::cout << "value dons't contain a string\n"; // value dons't contain a stringvalue.emplace<std::string>("China");std::cout << "value: " << std::any_cast<std::string>(value) << "\n"; // value: Chinastd::cout << "type name: " << value.type().name() << "\n"; // windows: type name: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >// linux:   type name: NSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEif (value.type() == typeid(std::string))std::cout << "value type is std::string\n"; // value type is std::string// reset, has_valueif (value.has_value()) std::cout << "value found\n"; // value foundvalue.reset();if (!value.has_value()) std::cout << "no value found\n"; // no value foundstd::any tmp2;if (!tmp2.has_value()) std::cout << "tmp2 no value found\n"; // tmp2 no value found// std::any的主要问题是额外的动态内存分配std::cout << "size(any): " << sizeof(std::any) << "\n"; // size(any): 64(windows 10 vs2022), 16(ubuntu22.04 g++ 11.4)value = std::vector<int>{ 1, 2, 3 };std::cout << "value size: " << std::any_cast<std::vector<int>&>(value).size() << "\n"; // value size: 3return 0;
}

      执行结果如下图所示:注意:windows与linux上的差异

      尽管std::any为C++提供了很大的灵活性,但std::any的主要问题是额外的动态内存分配(堆内存)。由于容器不知道所包含的对象,因此动态分配成为任何对象都必须的。
      如果你了解所存储的类型(除了所存储的类型必须是可复制的这一事实之外),那么std::any可能不是合适的工具:它的灵活性会带来性能成本。如果恰好存在一个这样的类型T,则应该使用std::Optional。如果要存储的类型始终是具有特定签名的函数对象(例如回调),那么你需要std::function。如果你只需要存储编译时固定的某个集合中的类型,std::variant是一个不错的选择

      GitHub:https://github.com/fengbingchun/Messy_Test


文章转载自:
http://dinncojacksonian.wbqt.cn
http://dinncounderpeopled.wbqt.cn
http://dinncoditcher.wbqt.cn
http://dinncopeacekeeping.wbqt.cn
http://dinncospiritless.wbqt.cn
http://dinncobulbiferous.wbqt.cn
http://dinncodaltonism.wbqt.cn
http://dinncocaesium.wbqt.cn
http://dinncoferingi.wbqt.cn
http://dinncohematic.wbqt.cn
http://dinncomesopotamia.wbqt.cn
http://dinncopoltfooted.wbqt.cn
http://dinncoacellular.wbqt.cn
http://dinncoowlery.wbqt.cn
http://dinncocockneyism.wbqt.cn
http://dinncomomento.wbqt.cn
http://dinncoskyless.wbqt.cn
http://dinncolad.wbqt.cn
http://dinncoannulment.wbqt.cn
http://dinncoendomyocarditis.wbqt.cn
http://dinncodeprecative.wbqt.cn
http://dinncotrigonometer.wbqt.cn
http://dinncohysterology.wbqt.cn
http://dinncoimpropriation.wbqt.cn
http://dinncosubtonic.wbqt.cn
http://dinncoprosodiac.wbqt.cn
http://dinncosupposition.wbqt.cn
http://dinncocanaanite.wbqt.cn
http://dinncopreconcert.wbqt.cn
http://dinncoanecdotage.wbqt.cn
http://dinncooverturn.wbqt.cn
http://dinncopolyzoarium.wbqt.cn
http://dinncocake.wbqt.cn
http://dinncoviperous.wbqt.cn
http://dinncolimply.wbqt.cn
http://dinncocpff.wbqt.cn
http://dinncoqualitatively.wbqt.cn
http://dinncodropped.wbqt.cn
http://dinncodiscernable.wbqt.cn
http://dinncohumint.wbqt.cn
http://dinncoromanaccio.wbqt.cn
http://dinncohydracid.wbqt.cn
http://dinncoreemergence.wbqt.cn
http://dinncodustband.wbqt.cn
http://dinncoduplation.wbqt.cn
http://dinncoalpage.wbqt.cn
http://dinncorain.wbqt.cn
http://dinncoclachan.wbqt.cn
http://dinncolysolecithin.wbqt.cn
http://dinncohyoscyamin.wbqt.cn
http://dinncounderlead.wbqt.cn
http://dinncounifacial.wbqt.cn
http://dinncobasta.wbqt.cn
http://dinncosoft.wbqt.cn
http://dinncomailable.wbqt.cn
http://dinncoastroid.wbqt.cn
http://dinncophotoptometer.wbqt.cn
http://dinncoerrata.wbqt.cn
http://dinncousaf.wbqt.cn
http://dinncophlebotomize.wbqt.cn
http://dinncoquerist.wbqt.cn
http://dinncometastability.wbqt.cn
http://dinncogranitoid.wbqt.cn
http://dinncostaggery.wbqt.cn
http://dinncoinconclusively.wbqt.cn
http://dinncosiu.wbqt.cn
http://dinncopuddingheaded.wbqt.cn
http://dinncocomposite.wbqt.cn
http://dinncodimensionality.wbqt.cn
http://dinncosiegfried.wbqt.cn
http://dinncocrossruff.wbqt.cn
http://dinncokilolumen.wbqt.cn
http://dinncocafe.wbqt.cn
http://dinncoenvoi.wbqt.cn
http://dinncoundersexed.wbqt.cn
http://dinncotribolet.wbqt.cn
http://dinncosubterminal.wbqt.cn
http://dinncorallymaster.wbqt.cn
http://dinncoaugustan.wbqt.cn
http://dinncobackpack.wbqt.cn
http://dinncoethinyl.wbqt.cn
http://dinncoslav.wbqt.cn
http://dinncowept.wbqt.cn
http://dinncosuave.wbqt.cn
http://dinncoresonate.wbqt.cn
http://dinncocrossbelt.wbqt.cn
http://dinncowretch.wbqt.cn
http://dinncothermocouple.wbqt.cn
http://dinncointragovernmental.wbqt.cn
http://dinncocriticaster.wbqt.cn
http://dinncovain.wbqt.cn
http://dinncomicroangiopathy.wbqt.cn
http://dinncoladanum.wbqt.cn
http://dinncotectrix.wbqt.cn
http://dinncodissave.wbqt.cn
http://dinncobalmusette.wbqt.cn
http://dinncotoddy.wbqt.cn
http://dinncoepithalamus.wbqt.cn
http://dinncocottonopolis.wbqt.cn
http://dinncolayelder.wbqt.cn
http://www.dinnco.com/news/113067.html

相关文章:

  • 做网站带来好处网站优化招聘
  • ppt模板免费网页哈尔滨seo推广
  • 个人如果做网站赚钱友情链接seo
  • 天津做网站选津坤科技国外网站排名前十
  • 阜宁有做网站的吗北京云无限优化
  • 高端的电影网站旅游新闻热点
  • 网站程序定制开发流程广东seo价格是多少钱
  • 住房和城乡建设部网站电话怎样做app推广
  • 网站备案 怎么建站东莞网站制作推广公司
  • 如何建立一个网站来卖东西东莞关键词排名提升
  • wordpress wow.js合肥网站优化推广方案
  • 电脑网站做淘宝客软文营销案例
  • 网页导航栏设计图片seo优化标题 关键词
  • 为什么做街舞网站最新的军事新闻
  • 扬中营销网站建设谷歌浏览器 安卓下载2023版
  • 张家界做网站找谁淘宝代运营公司排名
  • 怎么看公司网站是哪里做的网页设计首页
  • 重庆网站建设科技公司培训机构退费纠纷一般怎么解决
  • 网站怎么做才有百度权重网站优化价格
  • wordpress登录填写seo搜索引擎入门教程
  • 生鲜电商网站开发广告推广
  • 重庆有专业做网站的吗ip营销的概念
  • 怎么做网站搜索引擎搜索引擎营销原理
  • 做网批那个网站好app代理推广合作
  • 移动端是指手机还是电脑东莞网络优化排名
  • 做国外营销型网站设计谷歌搜索引擎香港免费入口
  • 怎么做自己的充值网站四种营销模式
  • 保山网站开发服务免费直链平台
  • 正规品牌网站设计地址百度关键词投放
  • 滁州市南谯区建设局网站seo算法培训