Metafunction is a more general idiom than type generator. Metafunctions that produce type(s) as a result is used as type generators.

Appearance:

  • a class template in which all the parameters are types
  • a class with publicly accessible type type

Intent

  • To encapsulate a complex type computation algorithm
  • To generate a type using compile-time type selection tech

Metafunction idiom is the principal way of writing compile-time algorithms in C++.

Implementation of metafunctions is often based on template specializations.

Example

  1. template<bool, class L, class R>
  2. struct IF{
  3. typedef R type;
  4. };
  5. template<class L, class R>
  6. struct IF<true, L, R>{
  7. typedef L type;
  8. };
  9. IF<false, int, long>::type i; // long i
  10. IF<true, int, long>::type i; // int i

Reference

More C++ Idioms/Metafunction

Metafunction的更多相关文章

  1. Python2.6-原理之类和oop(下)

    来自<python学习手册第四版>第六部分 五.运算符重载(29章) 这部分深入介绍更多的细节并看一些常用的重载方法,虽然不会展示每种可用的运算符重载方法,但是这里给出的代码也足够覆盖py ...

  2. 一道模板元编程题源码解答(replace_type)

    今天有一同学在群上聊到一个比较好玩的题目(本人看书不多,后面才知是<C++模板元编程>第二章里面的一道习题), 我也抱着试一试的态度去完成它, 这道题也体现了c++模板元编程的基础和精髓: ...

  3. C# Windows Service服务的创建和调试

    前言 关于Windows服务创建和调试的文章在网络上的很多文章里面都有,直接拿过来贴在这里也不过仅仅是个记录,不会让人加深印象.所以本着能够更深刻了解服务项目的创建和调试过程及方法的目的,有了这篇记录 ...

  4. 可爱的 Python : Python中函数式编程,第二部分

    英文原文:Charming Python: Functional programming in Python, Part 2,翻译:开源中国 摘要:  本专栏继续让David对Python中的函数式编 ...

  5. 千行代码入门Python

    这个是从网上找到的一份快速入门python的极简教程,大概一千行左右,个人觉得不错,特此收藏以备后用. # _*_ coding: utf-8 _*_ """类型和运算- ...

  6. More C++ Idioms

    Table of Contents Note: synonyms for each idiom are listed in parentheses. Adapter Template TODO Add ...

  7. 2D and 3D Linear Geometry Kernel ( Geometry Kernels) CGAL 4.13 -User Manual

    1 Introduction CGAL, the Computational Geometry Algorithms Library, is written in C++ and consists o ...

  8. Lua只读表

    利用Lua的元表(metatable)和元函数(metafunction)可以很简单的实现此功能. 其实现大致分为三个部分 1.禁止在表中创建新值 2.禁止改变已有的值 3.将子表也变为只读 1.禁止 ...

  9. Python Syntax Summary

    # _*_ coding: utf-8 _*_ """########################################################## ...

随机推荐

  1. ScriptManager需要用到的JS

    <script type="text/javascript"> Sys.Application.add_load(function() { var form = Sys ...

  2. dojo.hasClass/dojo.addClass/dojo.removeClass/dojo.toggleClass/dojo.repalceClass

    dojo.hasClass(/*DomNode*/node or DomIdstring,/*String*/classString)//如果节点中有特定的类,那么返回ture,否则返回false d ...

  3. cocoapods导入第三方库

    1.移除现有Ruby默认源 终端:gem sources --remove https://rubygems.org/ 2.使用新的源 终端:gem sources -a https://ruby.t ...

  4. iOS FMDB

    FMDB FMDB概述 什么是FMDB * FMDB是iOS平台的SQLite数据库框架 * FMDB以OC的方式封装了SQLite的C语言API FMDB的优点 * 使用起来更加面向对象,省去了很多 ...

  5. SQL使用记录

    Q:怎么删掉sql server登录时的用户名?(仅仅是删掉那个登录时的提示) A:先关闭数据库登录引擎,然后删除:%AppData%\Microsoft\Microsoft SQL Server\1 ...

  6. Struct和Class的区别

    转载至:http://blog.csdn.net/yuliu0552/article/details/6717915 C++中的struct对C中的struct进行了扩充,它已经不再只是一个包含不同数 ...

  7. phpstorm8 配置svn

    步骤1 步骤2. 步骤3.

  8. iphone iOS7恢复到iOS6教程

    步骤一:首先根据您iOS设备型号,下载最新的iOS 6固件,您可以进入苹果官网下载,也可以以下网址下载. http://sj.zol.com.cn/ios613/

  9. Fiddler 域名过滤

    原来一直没意识到Fiddler过滤,导致每次抓包都要自己判断.搜索好多东西,真是呵呵! 过滤设置很简单,看懂一张图就解决问题了. 箭头 那两处设置下,圆圈处保存再进行抓包即可

  10. MYSQL 为表指定文件位置 data directory

    背景知识: 如果表不指定文件位置,它会保存到 data/database_name/table_file;其中data在你指定的安装目录下,为了提高IO我们尽可能的 用到多个硬盘的IO能力,这个就需要 ...