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 ...
随机推荐
- jmeter笔记5
性能测试是任何分布式或Web应用程序测试计划的重要组成部分.在计划和开发周期中进行性能评价,可以保证交付给客户的应用程序满足客户对于高负载.可用性和可伸缩性的要求.提前确定软件的负载限制可以为适当地进 ...
- Compound Interest Calculator1.0
Compound Interest Calculator1.0 客户说:帮我开发一个复利计算软件. 计算:本金为100万,利率或者投资回报率为3%,投资年限为30年,那么,30年后所获得的利息收入:按 ...
- Qt之QSystemTrayIcon
简述 QSystemTrayIcon类为应用程序在系统托盘中提供一个图标. 现代操作系统通常在桌面上提供一个特殊的区域,称为系统托盘或通知区域,长时间运行的应用程序可以显示图标和短消息. 简述 内容 ...
- 229. Majority Element II -- 找出数组中出现次数超过 ⌊ n/3 ⌋ 次的数
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...
- Hadoop概括——学习笔记<一>转
前言 第一章主要讲的是hadoop基础知识.老师讲的还是比较全面简单的,起码作为一个非专业码农以及数据库管理人员,也能狗大致了解其特点 首先是概括图(以hadoop2.0为例) 一.Hadoop基础 ...
- easyui 中重复加载两次url
之前一直在使用easyui中,忽视了官网上的小细节,类似于datagrid.combobox 等组件在使用的时候,它的数据加载方式分为两种: 官网中: ①在html中,比如: <table id ...
- Cassandra——类似levelDB的基于p2p架构的分布式NOSQL数据库
C: Consistency 一致性 • A: Availability 可用性(指的是快速获取数据) • P: Tolerance of network Partition 分区容忍性(分布式) 1 ...
- xll
http://www.aiuxian.com/article/p-2027873.html
- AlarmManager手机闹钟简介
1.void set(int type , long triggerAtTime , PendingIntent operation ) : 设置在 triggerAtTime时间启动由operati ...
- java模式之-模板方法模式
模板方法模式是java设计模式常见的模式之一. <JAVA与模式>中写道: 模板方法模式是类的行为模式.准备一个抽象类,将部分逻辑以具体方法以及具体构造函数的形式实现,然后声明一些抽象方法 ...