模板方式方法转换类型报错问题

文章讨论了一个模板函数printJsonType在Windows平台上的编译问题,该问题涉及到函数指针类型FunType与void类型的不匹配。通过将FunType&func修改为constFunType&func解决了这个问题。此外,文章指出使用lambda表达式不会触发同样的编译错误。
摘要由CSDN通过智能技术生成

1.问题描述
template <typename FunType, typename… Args>
void printJsonType(Json& buffer_json,FunType& func,Args&& …args){

func(std::forward(args)…);
}
自定义方法去调用
void printDemo(std::string ss){

}

printJsonType(json,printDemo,"test");

在windows平台编译会报错:
无法将参数  从“void (__cdecl *)(Json &,std::string )”转换为FunType &” 

with [
FunType=void (__cdecl *)(std::string)
]

2.解决方式:
将FunType& func 改为 const FunType& func

template <typename FunType, typename… Args>
void printJsonType(Json& buffer_json,const FunType& func,Args&& …args){

func(std::forward(args)…);
}

3.使用lambda方式不会出现1的编译错误。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值