type safe printf
在书里看到的,摘录如下:
#include <iostream>
#include <stdexcept> template<class T> struct is_C_style_string:std::false_type{};
template<> struct is_C_style_string<char*>:std::true_type{};
template<> struct is_C_style_string<const char*>:std::true_type{};
void printf_ts(const char* s){
if(s == nullptr)return;
while(*s){
if(*s=='%' && *++s!='%')
throw std::runtime_error("invalid format:missing arguments");
std::cout << *s++;
}
} template<typename T, typename... Args>
void printf_ts(const char*s, T value, Args... args){
while(s && *s){
if(*s == '%' && *++s != '%'){
std::cout << value;
return printf_ts(++s, args...);
}
std::cout << *s++;
}
throw std::runtime_error("Extra arguments provided to printf_ts");
}; template<typename T, typename... Args>
void printf_ts(const char* s, T value, Args... args){
while(s && *s){
if(*s == '%'){
switch(*++s){
case '%':
break;
case 's':
if(!is_C_style_string<T>::value)
throw std::runtime_error("Bad printf() format");
break;
case 'd':
if(!std::is_integral<T>::value)
throw std::runtime_error("Bad printf() format");
break;
case 'g':
if(!std::is_floating_point<T>::value)
throw std::runtime_error("Bad printf() format");
break;
}
std::cout << value;
return printf_ts(++s, args...);
}
std::cout << *s++;
}
throw std::runtime_error("extra arguments provided to printf");
}; int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
}
type safe printf的更多相关文章
- awesome-modern-cpp
Awesome Modern C++ A collection of resources on modern C++. The goal is to collect a list of resouce ...
- ##C++ format 格式化字符串
C++ format 格式化字符串实现方式 1. http://stackoverflow.com/questions/2342162/stdstring-formatting-like-sprint ...
- 关于type check的定义
Concept: Type Checking There is no static type checking in Scheme; type checking is done at run time ...
- Type Safety and Type Inference
Swift is a type-safe language. A type safe language encourages you to be clear about the types of va ...
- Type Systems
This section deals with more theoretical aspects of types. A type system is a set of rules used by a ...
- golang --- fmt.printf I/O 函数格式化说明
说明 fmt 包实现了格式化 I/O 函数,类似于 C 的 printf 和 scanf格式“占位符”衍生自 C,但比 C 更简单 常用格式化输出 fmt.Printf("start at ...
- Golang fmt Printf 格式化参数手册/详解/说明
fmt 包实现了格式化I/O函数,类似于C的 printf 和 scanf. 格式“占位符”衍生自C,但比C更简单. fmt 包的官方文档对Printing和Scanning有很详细的说明.这里就直接 ...
- 了解一下C++输入和输出的概念
我们经常用到的输入和输出,都是以终端为对象的,即从键盘输入数据,运行结果输出到显示器屏幕上.从操作系统的角度看,每一个与主机相连的输入输出设备都被看作一个文件.除了以终端为对象进行输入和输出外,还经常 ...
- Google C++ Style Guide
Background C++ is one of the main development languages used by many of Google's open-source project ...
随机推荐
- 《Java程序设计》实验五 实验报告
实验五 java网络编程 实验内容 XP基础 XP核心实践 相关工具 实验要求 1.没有Linux基础的同学建议先学习<Linux基础入门(新版)><Vim编辑器> 课程 2. ...
- Jdk1.8+Eclipse+MySql+Tomcat开发Java应用的环境搭建
Java学习开发的入门教程,方便大家在学习java开发过程中掌握最基本的环境搭建 有视频,有真相 http://www.chuanke.com/1340360-164338.html jdk是操作系统 ...
- 用XmlSerializer进行xml反序列化的时候,程序报错: 不应有 <xml xmlns=''>
原因 一,类型错误: 比如xml本来是UserInfo类型 用XmlSerializer进行反序列化传入的类型是MemberInfo这就会报错 二,xml根节点和对象的类名不一致,而又没有对类加入[X ...
- BZOJ1962 模型王子
戳这里 /************************************************************** Problem: 1962 User: rausen Langu ...
- mysql把查询的结果格式成日期
SELECT *,FROM_UNIXTIME(addtime, '%Y-%m-%d %H:%i:%S') as riqi FROM `test`
- intel vt-x处于禁用状态下如何处理
1.首先看你的bios选项里面有没有该选项,如果没有就更新,更新之后还没有,则不支持 2.找到intel Virtualization Technology 将状态改为Enabled 同时找到int ...
- PHP分页类库
<?php /** * @title: Ekcms page分页类库 * @version: 1.0 * @author: perry <perry@1kyou.com> * @pu ...
- qml的打包问题
qml2的打包问题: 相对于早期的项目,只需要打包plugin和动态库.带有sqlite的程序如果需要打包,需要打包如下东西: 1.打包AppData目录下的 Local/Qt Project/项目 ...
- hdu 4638 Group
http://acm.hdu.edu.cn/showproblem.php?pid=4638 问题其实就是求[L,R]中有多少个连续的段 若每一个人都是一个段 那么[L,R]中每一个朋友关系就会减少一 ...
- ROS程序编辑器
我找到的比较好用的ROS代码编辑器,对于emacs和vim等神级编辑器不能自动补全,对于我这种新手编译出错都是字母打错了, 因此果断回避,找到了一款叫做code blocks的编辑器,在软件中心就能下 ...