template<typename Function, typename Tuple, std::size_t... Index>
decltype(auto) invoke_impl(Function&& func, Tuple&& t, std::index_sequence<Index...>)
{
return func(std::get<Index>(std::forward<Tuple>(t))...);
} template<typename Function, typename Tuple>
decltype(auto) invoke(Function&& func, Tuple&& t)
{
constexpr auto size = std::tuple_size<typename std::decay<Tuple>::type>::value;
return invoke_impl(std::forward<Function>(func), std::forward<Tuple>(t), std::make_index_sequence<size>{});
}

使用:

int add(int a, int b)
{
return a + b;
} int main()
{
std::tuple<int, int> t = std::make_tuple(1, 2);
std::cout << invoke(add, t) << std::endl;
return 0;
}

这里用到了C++14的std::index_sequence,std::index_sequence很有用,它可以将std::array和std::tuple转换成序列。

std::tuple作为参数invoke调用函数的更多相关文章

  1. [转]JavaScript通过参数动态调用函数——js中eval实现反射

    以下文章出自  http://blog.rongzhiwang.com/king/archive/2012/08/13/javascriptjseval.aspx       今天碰到人问这样一个问题 ...

  2. 父窗口和iframe子窗口之间相互传递参数和调用函数或方法

    1.父窗口向子窗口传递参数: 可以在url中添加参数:2.html?a=1&b=2&c=3 然后在子页面上可用js解析,提供一个函数: function getQueryStr(sAr ...

  3. python定义函数时的参数&调用函数时的传参

    一.定义函数: 1.位置参数:直接定义参数 2.默认参数(或者关键字参数):参数名 = "默认值" 3.位置参数必须在默认参数之前 二.调用函数: 1.按位置传,直接写参数的值 2 ...

  4. Python(调用函数、定义函数)

    函数的返回值: return 值:只能返回一次,只要执行return函数就终止 返回值:没有类型限制,也没有个数限制 没有return:None 返回一个值 返回多个值:元组 先定义,后使用,定义阶段 ...

  5. 11、Python函数基础(定义函数、函数参数、匿名函数)

    函数先定义函数,后调用 一.定义函数: 1.简单的规则: 函数代码块以 def 关键词开头,后接函数标识符名称和圆括号 (). 任何传入参数和自变量必须放在圆括号中间,圆括号之间可以用于定义参数. 函 ...

  6. JavaScript 使用new关键字调用函数

    使用new关键字调用函数 test.js 代码如下 function Person(name, age, obj) { var o = new Object(); o.name = name; o.a ...

  7. Python调用函数带括号和不带括号的区别

    1.不带括号时,调用的是这个函数本身 ,是整个函数体,是一个函数对象,不需等该函数执行完成 2.带括号(此时必须传入需要的参数),调用的是函数的return结果,需要等待函数执行完成的结果 如果函数本 ...

  8. Python函数的定义、参数传入与函数的调用

    作为计算机代码的一种抽象方式,函数在Python中扮演了极为重要的角色.今天给大家介绍Python函数的定义.参数的传入以及调用方式.其中函数参数的传入方式为本节重点内容.Python函数的参数形式包 ...

  9. Python基础笔记:函数:调用函数、定义函数、函数的参数、递归函数

    一.定义一个求二元一次方程的根的函数 #Sublime Text import math def ee(a,b,c): delta=b*b-4*a*c if delta<0: return 'n ...

随机推荐

  1. HYSBZ 1036(树的统计Count)

    题目链接:传送门 题目大意:中文题,略 题目思路:树链剖分裸题. 闲谈:树链越练越熟练了 #include <iostream> #include <cstdio> #incl ...

  2. Android 将时间戳转为代表"距现在多久之前"的字符串

    public String getStandardDate(int dateTime) { StringBuffer sb = new StringBuffer(); long t = Long.pa ...

  3. HDU_5510_Bazinga

    Bazinga Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Sub ...

  4. 透明 Transparent connections through HTTP proxies.

    透明语境: 5.7层模型中数据链路层:透明传输: 谈谈如何使用Netty开发实现高性能的RPC服务器 - Newland - 博客园 http://www.cnblogs.com/jietang/p/ ...

  5. [报错]Fast enumeration variables cannot be modified in ARC by default; declare the variable __strong to allow this

    今天写了下面的快速枚举for循环代码,从按钮数组subButtons中取出button,然后修改button的样式,在添加到view中 for (UIButton *button in subButt ...

  6. MetaClass

    它的作用主要是 指定由谁来创建类,默认是type #python3 class Foo(metaclass=MyType): pass #python2 class Foo(object): __me ...

  7. What’s wrong with virtual methods called through an interface

    May 31, 2016 Calling a virtual method through an interface always was a lot slower than calling a st ...

  8. TypeError: cannot use a string pattern on a bytes-like object的解决办法

    #!/usr/python3 import re import urllib.request def gethtml(url): page=urllib.request.urlopen(url) ht ...

  9. icomoon.io生成字体图标

    1. 准备svg图片 2. 打开icomoon选择icomoon App 3. import icons 上传本地的svg图片 4. 点击选中以后点击generate fonts形成字体图标 5. p ...

  10. Redis应用案例,查找某个值的范围(转)

    原文:https://groups.google.com/forum/#!topic/redis-db/lrYbkbxfQiQ 本文来自Redis在Google Group上的一个问题,有一位同学发贴 ...