参数自然匹配

#include <type_traits>

#include <utility>

struct A {}; struct B {}; struct C {};

void func(A, const C&, B) {}

template<typename func_return_type, typename ...func_parameter, typename ...input_type>

decltype(auto) run(func_return_type(func_parameter...), input_type&&...);

template<typename ...type> struct type_packet {};

template<size_t cur, typename output, typename input, typename target> struct adapter_execute;

template<typename input, typename target> struct adapter

{

using result = typename adapter_execute<0, std::index_sequence<>, input, target>::result;

};

template<size_t ...input_index, typename func_ret, typename ...fun_para, typename ...input_parameter>

decltype(auto) func_execute(std::index_sequence<input_index...>, func_ret(f)(fun_para...), input_parameter&&... ip);

template<typename func_return_type, typename ...func_parameter, typename ...input_type>

decltype(auto) run(func_return_type(f)(func_parameter...), input_type&&... it)

{

return func_execute(

typename adapter<type_packet<input_type...>, type_packet<func_parameter...> >::result(),

f,

std::forward<input_type>(it)...);

}

template<size_t i> struct pick_parameter

{

template<typename T, typename ...AT>

static decltype(auto) pick(T&& t, AT&& ...at) { return pick_parameter<i - 1>::pick(std::forward<AT>(at)...); }

};

template<> struct pick_parameter<0>

{

template<typename T, typename ...AT>

static decltype(auto) pick(T&& t, AT&& ...at) { return std::forward<T>(t); }

};

template<size_t ...input_index, typename func_ret, typename ...fun_para, typename ...input_parameter>

decltype(auto) func_execute(std::index_sequence<input_index...>, func_ret(f)(fun_para...), input_parameter&&... ip)

{

return (f)(pick_parameter<input_index>::pick(std::forward<input_parameter>(ip)...)...);

}

template<size_t cur, size_t ...output, typename ...input, typename ...target>

struct adapter_execute<cur, std::index_sequence<output...>, type_packet<input...>, type_packet<target...>>

{

using result = std::index_sequence<output...>;

};

template<size_t cur, size_t ...output, typename this_input, typename ...input, typename ...target>

struct adapter_execute<cur, std::index_sequence<output...>, type_packet<this_input, input...>, type_packet<target...>>

{

using result = std::index_sequence<output...>;

};

template<size_t cur, size_t ...output, typename this_input, typename ...input, typename this_target, typename ...target>

struct adapter_execute<cur, std::index_sequence<output...>, type_packet<this_input, input...>, type_packet<this_target, target...>>

{

using result = typename std::conditional_t<

std::is_constructible<this_target, this_input>::value,

adapter_execute<cur+1, std::index_sequence<output..., cur>, type_packet<input...>, type_packet<target...>>,

adapter_execute<cur+1, std::index_sequence<output...>, type_packet<input...>, type_packet<this_target, target...>>

>::result;

};

int main()

{

run(func,A(),B(),C(),C(),B(),A());

}

想象一个fp的类型队列,里面装着函数的参数类型,另一个ip的类型队列,里面保存着输入参数的类型。还需要一个队列保存位置,和一个保存当前ip位置的临时变量cur。

算法很简单,提取fp的第一个类型,然后提取ip的第一个类型,如果能够匹配,则将cur放入位置队列的末尾,并且将fp的第一个类型弹出队列。无论是否匹配,都将ip的第一个类型弹出,然后将cur+1。

时间: 2024-08-07 16:39:18

参数自然匹配的相关文章

MFC CFileDialog派生类在编译过程出现“error 没有与参数列表匹配的构造函数......”的问题

CFileDialog派生类在编译过程出现"error 没有与参数列表匹配的构造函数实例参数类型为(int,const char[4],const char[6],int,const char[36],int)  .的问题 解决办法 右击项目---属性----配置属性-----常规-----字符集  选择"使用多字节字符集"即可~~~~撒花~~

Java开发WebServices传递pojo提示参数不匹配的问题

webservices服务用Java开发的时候,参数为基本数据类型是没有什么问题的. 但是如果是传递pojo,也就是java对象,可能你会遇到参数不匹配的问题. webservices接口暴露出来以后肯定是一个wsdl文件,看到网上很多文章说"webservices传递pojo的时候,提示参数不匹配,很多人解决方式是把接口和pojo放在一起就好用了.这个其实是表象" This XML file does not appear to have any style information

C/C++函数指针参数不匹配问题

函数指针在C/C++中很常用,最近遇到了一非常有趣的问题,函数指针的参数和函数的形参个数不匹配时会遇到的问题,下面看代码. // // main.cpp // FuncPointer // // Created by ChengChao on 14-9-21. // Copyright (c) 2014年 cc. All rights reserved. // #include <iostream> using namespace std; int add(int a, int b) { re

springboot整合rabbirmq中使用mandatory参数获取匹配失败的消息!

先说下这个参数的作用: /** * Mandatory为true时,消息通过交换器无法匹配到队列会返回给生产者 * 为false时,匹配不到会直接被丢弃 */在一些特定场景下还是有用处的!接下来说一下绑定队列与交换器,需要在配置类或者xml中提前配置好尤其是queue,如果同时写了消费者,必须先配置好bean,即mq中队列必须存在,不然会报错 //创建消息队列 @Bean public Queue testQueue(){ //boolean表示消息是否持久化 return new Queue(

pthread_create 报函数参数不匹配问题

pthread_create方法遇到类方法时总会报  argument of type 'void* (Thread::)(void*)' does not match 'void* (*)(void*)'pthread_create方法第三个参数只能是C函数指针或者类到静态函数指针.下面记录一下解决方法 1 include <stdio.h> 2 #include <pthread.h> 3 #include <unistd.h> 4 5 class Thread{

没有与参数列表匹配的 重载函数 AfxMessageBox()

解决方案: 尝试下使用 _T: AfxMessageBox(_T("请输入名称!")); 可能你使用的VC版本比较高,采用的是宽字符吧,一般这种情况下我们在字符串前加 L: AfxMessageBox(L"请输入名称!"); 或TEXT AfxMessageBox(TEXT("can not store it")); 但为了程序的适用性,使用_T更好些.因为用_T会自动按你程序所在环境来决定是否是宽字符还是简单的ASCII,省事啊!

利用DetachedCriteria构建HQL参数动态匹配

此文章是基于 搭建SpringMVC+Spring+Hibernate平台 1. DetachedCriteria构建类:CriteriaBuilder.java package com.ims.persistence.base; import java.math.BigDecimal; import java.sql.Types; import java.util.Map; import org.apache.commons.lang3.StringUtils; import org.hibe

报错:没有与参数列表匹配的构造函数 &quot;CFileDialog::CFileDialog&quot; 实例

如果是在解决方案管理器窗口内,右击你的项目“项目”,然后选“属性”(最后一项),再点“配置属性”,是个“+”号,把它展开,然后选“常规”选项卡,倒数第三项“字符集”,选择“使用多字节字符集”.再编译应该就可以了.

搞定多线程自动代码生成控件的问题,解决了参数不匹配异常

直接帖核心代码 private void button1_Click(object sender, EventArgs e)        {            this.tableLayoutPanel1.Controls.Clear();            this.tableLayoutPanel1.RowCount = 10;            this.tableLayoutPanel1.ColumnCount = 10;            Button btn;