Boost.Hana

Boost.Hana 是一个元编程的库。它为不同种类数据的集合以及类型的集合提供了容器和算法。

  1. #include <boost/hana.hpp>
  2. namespace hana = boost::hana;
  3. #include <cassert>
  4. #include <iostream>
  5. #include <string>
  6. struct Fish { std::string name; };
  7. struct Cat { std::string name; };
  8. struct Dog { std::string name; };
  9. auto animals = hana::make_tuple(Fish{"Nemo"}, Cat{"Garfield"}, Dog{"Snoopy"});
  10. int main()
  11. {
  12. using namespace hana::literals;
  13. // Access tuple elements with operator[] instead of std::get.
  14. Cat garfield = animals[1_c];
  15. // Perform high level algorithms on tuples (this is like std::transform)
  16. auto names = hana::transform(animals, [](auto a) {
  17. return a.name;
  18. });
  19. assert(hana::reverse(names) == hana::make_tuple("Snoopy", "Garfield", "Nemo"));
  20. auto animal_types = hana::make_tuple(hana::type_c<Fish*>, hana::type_c<Cat&>, hana::type_c<Dog>);
  21. auto no_pointers = hana::remove_if(animal_types, [](auto a) {
  22. return hana::traits::is_pointer(a);
  23. });
  24. static_assert(no_pointers == hana::make_tuple(hana::type_c<Cat&>, hana::type_c<Dog>), "");
  25. auto has_name = hana::is_valid([](auto&& x) -> decltype((void)x.name) { });
  26. static_assert(has_name(garfield), "");
  27. static_assert(!has_name(1), "");
  28. }

Boost.Hana的更多相关文章

  1. Boost.Hana在visual studio 2017 rc中的残缺使用

    最新的visual studio还不支持hana,不知道vs2017正式版本出后会不会支持.等不及了,先用rc版试试吧. 1.从https://github.com/boostorg/hana下载或拉 ...

  2. zz A list of open source C++ libraries

    A list of open source C++ libraries < cpp‎ | links http://en.cppreference.com/w/cpp/links/libs Th ...

  3. C++模板杂谈

    在模板编程中,有几个常用的技术:模板(偏)特化,特性萃取,标签分派,匹配失败不是错误.其中模板(偏)特化是基础,匹配失败不是错误(SFINAE)应用最为广泛. 现代C++对模板编程做了更多的加强,bo ...

  4. C++17尝鲜:variant

    variant variant 是 C++17 所提供的变体类型.variant<X, Y, Z> 是可存放 X, Y, Z 这三种类型数据的变体类型. 与C语言中传统的 union 类型 ...

  5. awesome-modern-cpp

    Awesome Modern C++ A collection of resources on modern C++. The goal is to collect a list of resouce ...

  6. An Introduction to Reflection in C++

    Apr 13, 2017 Stop me if you’ve heard this one before. You are working on a messaging middleware, a g ...

  7. C++17尝鲜

    https://cloud.tencent.com/developer/article/1351910 [译]C++17,optional, any, 和 variant 的更多细节 用户261520 ...

  8. Boost 1.61.0 Library Documentation

    http://www.boost.org/doc/libs/1_61_0/ Boost 1.61.0 Library Documentation Accumulators Framework for ...

  9. boost强分类器的实现

    boost.cpp文件下: bool CvCascadeBoost::train( const CvFeatureEvaluator* _featureEvaluator, int _numSampl ...

随机推荐

  1. jvm 知识点

    双亲委派模型的工作流程是: 如果一个类加载器收到了类加载的请求,它首先不会自己去尝试加载这个类,而是把请求委托给父加载器去完成,依次向上,因此,所有的类加载请求最终都应该被传递到顶层的启动类加载器中, ...

  2. IE8下部分方法失效的解决方法

    1.IE8下String的Trim()方法失效的解决方法 用jquery的trim()方法,$.trim(str)就可以了: 例:_id.trim() !='' 改为  $.trim(_id) != ...

  3. jmeter --自动化badboy脚本开发技术

    jmeter --自动化badboy脚本开发技术 一般人用badboy都是使用它的录制功能,其它badboy还是一款自动化的工具,它可以实现检查点.参数化.迭代.并发.报告.断点等功能.本文就这些功能 ...

  4. ionic使用常见问题(八)——PHP无法获取$http的post数据

    一个简单的post请求 $http.post('do-submit.php',myData) .success(function(){ // some code });   可是,用angularjs ...

  5. CentOS Linux解决Device eth0 does not seem to be present 但是没有发现eth1

    http://www.linuxidc.com/Linux/2012-12/76248.htm 此标题已经是有人写过的了.但是为什么拿来重写? 我复制完,没有发现有eth1这个网卡 为什么呢?需要选中 ...

  6. BASIC-7_蓝桥杯_特殊的数字

    代码示例: #include <stdio.h>#define B(X) (X)*(X)*(X) int main(void){ int i = 0 ; int a = 0 , b = 0 ...

  7. bzoj1047 理想的正方形

    Description 有一个a*b的整数组成的矩阵,现请你从中找出一个n*n的正方形区域,使得该区域所有数中的最大值和最小值的差最小. Input 第一行为3个整数,分别表示a,b,n的值第二行至第 ...

  8. [转]html 下拉框级联

    <html> <head> <title>html 下拉框级联</title> <meta charset="UTF-8"/& ...

  9. vertical-align表单元素垂直对齐

    原文地址:http://www.blueidea.com/tech/web/2009/6910.asp 最近的项目涉及到很多表单的制作,特别是复选框(checkbox)和单选框(radio).但是在前 ...

  10. call和apply,bind的区别专讲

    可以干什么? 改变函数内的this指向: 什么时候使用? 构造函数使用this 为什么使用? 为了生成对象 类(函数名不可以带括号).call()      因为this指向对象,所以call的第一个 ...