第十六章 16.4.2节练习
来源:互联网 发布:云计算 编辑:程序博客网 时间:2024/04/28 11:38
练习16.56
编写并测试可变参数版本的errorMsg。
解答:
#include <iostream>using namespace std;template <typename T> ostream &print(ostream &os, const T &t){ return os << t; } template <typename T, typename... Args> ostream &print(ostream &os, const T &t, const Args& ... rset){ os << t << ", "; return print(os, rset...); } template <typename T>int hello(T a){ return 1;}template <typename... Args>ostream &errorMsg(ostream &os, const Args&... rset){ return print(os, hello(rset)...);}int main(){ errorMsg(cout, 1,2,3,4,5,6,7,8,9,10.9); cout << endl; errorMsg(cout, 17,8,9,10.9); cout << endl;}
练习16.57
比较你的可变参数版本的errorMsg和6.2.6节(第198页)中的error_msg函数。两种方法的优点缺点各是什么?
解答:
可变参的函数版本应该是能覆盖之前的那种形式。
之前的那种形式也就是同类型的initializer_list,序列中的元素必须是类型相同的。
而,本节的errorMsg更加的一般,能包容之前的形式,也能适应不同类型元素的打印。
0 0
- 第十六章 16.4.2节练习
- 第十六章 16.4节练习
- 第十六章 16.4.1节练习
- 第十六章 16.4.3节练习
- 第十六章 16.1.2节练习
- 第十六章 16.2.2节练习
- 第十六章 16.3节练习
- 第十六章 16.5节练习
- 第十六章 16.1.1节练习
- 第十六章 16.1.3节练习
- 第十六章 16.1.4节练习
- 第十六章 16.1.5节练习
- 第十六章 16.1.6节练习
- 第十六章 16.2.1节练习
- 第十六章 16.2.3节练习
- 第十六章 16.2.5节练习
- 第十六章 16.2.6节练习 & 16.2.7节练习
- c++primer第五版第十六章练习
- 单例模式
- 【内核研究】Framework常见问题
- Java重载
- 在线qq需要添加好友才能聊天的解决方法
- java.lang.NoClassDefFoundError: org/bouncycastle/jce/provider/BouncyCastleProvider解决方法
- 第十六章 16.4.2节练习
- python中yield用法
- [整理]Linux Shell概述
- 8.HelloWorld之使用Menu
- 线程同步:互斥量与读写锁的异同与性能分析
- android 录音和播放录音
- Selenium – Use Ant to Generate HTML Reports
- 赛瓦软件参加2014中国国际信息通信展
- js中document.write的那点事