lwqsbtl 2019-12-31 16:16 采纳率: 42.9%
浏览 222

C++ 模板编程之 包扩展?

template <typename T>
T work(T t){
    cout<<t<<endl;
    return t;
}

template <typename T> 
void print(const T &t){ 
    cout<<t<<endl;
}

template <typename T, typename... Args>
void  print(const T &t, const Args&... rest){    
    cout<<t<<endl;
    print(  work(rest)... );

        //这里,为什么调用上面的 print(  work(rest)... );可以实现对rest参数包的打印
        //而直接调用下面的work(rest)... 则会编译错误呢?    
    //work(rest)... ;  // 出错
}
int main(){
    print(1,0.1, 'c',string("str")) ;   
}  


template <typename... Args>
void  print_all(const Args&... rest){    
    //有没有办法通过类似下面的语句,直接实现对参数包所有参数的打印呢?
    //(cout<<rest)... ;
}
  • 写回答

1条回答 默认 最新

  • dxgzg 2020-05-19 20:04
    关注

    因为你的work函数里的参数不是const的,无法转换

    评论

报告相同问题?

悬赏问题

  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用