在书里看到的,摘录如下:

#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的更多相关文章

  1. awesome-modern-cpp

    Awesome Modern C++ A collection of resources on modern C++. The goal is to collect a list of resouce ...

  2. ##C++ format 格式化字符串

    C++ format 格式化字符串实现方式 1. http://stackoverflow.com/questions/2342162/stdstring-formatting-like-sprint ...

  3. 关于type check的定义

    Concept: Type Checking There is no static type checking in Scheme; type checking is done at run time ...

  4. 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 ...

  5. Type Systems

    This section deals with more theoretical aspects of types. A type system is a set of rules used by a ...

  6. golang --- fmt.printf I/O 函数格式化说明

    说明 fmt 包实现了格式化 I/O 函数,类似于 C 的 printf 和 scanf格式“占位符”衍生自 C,但比 C 更简单 常用格式化输出 fmt.Printf("start at ...

  7. Golang fmt Printf 格式化参数手册/详解/说明

    fmt 包实现了格式化I/O函数,类似于C的 printf 和 scanf. 格式“占位符”衍生自C,但比C更简单. fmt 包的官方文档对Printing和Scanning有很详细的说明.这里就直接 ...

  8. 了解一下C++输入和输出的概念

    我们经常用到的输入和输出,都是以终端为对象的,即从键盘输入数据,运行结果输出到显示器屏幕上.从操作系统的角度看,每一个与主机相连的输入输出设备都被看作一个文件.除了以终端为对象进行输入和输出外,还经常 ...

  9. Google C++ Style Guide

    Background C++ is one of the main development languages used by many of Google's open-source project ...

随机推荐

  1. linux笔记:文件处理命令touch,cat,more,less,head,tail

    命令名称:touch功能:新建文件命令所在目录:/bin/touch用法:touch 文件名 命令名称:cat功能:显示文件内容命令所在目录:/bin/cat用法:cat [-n] 文件名参数:-n ...

  2. 谈谈JPA-03-基本注解

    @Entity @Entity 标注用于实体类声明语句之前,指出该Java 类为实体类,将映射到指定的数据库表.如声明一个实体类 Customer,它将映射到数据库中的 customer 表上. @T ...

  3. Disable SELinux CentOS 7

    Disable SELinux CentOS 7 This blog covers the basic steps to disable SELinux on CentOS 7 first we ne ...

  4. 关于JVM的类型和模式

    原文出处: 摆渡者 引言 曾几何时,我也敲打过无数次这样的命令: 然而之前的我都只关心过版本号,也就是第一行的内容.今天,我们就来看看第3行输出的内容:JVM的类型和工作模式. 其实说Server和C ...

  5. jquery 获取属性的值

    jquery中用attr()方法来获取和设置元素属性,attr是attribute(属性)的缩写,在jQuery DOM操作中会经常用到attr(),attr()有4个表达式. 1.  attr( 属 ...

  6. wxpython颜色选择

    Color Code Color Name Color #000000 BLACK   #0000FF BLUE   #007FFF SLATE BLUE   #00FF00 GREEN   #00F ...

  7. STL中vector的用法

    vector是标准模板库的一种容器,是可存放各种类型的动态数组. #include<iostream> #include<vector> using namespace std ...

  8. 51nod 1021 石子归并(dp)

    51nod 1021 石子归并 题解:从i到j合并的最小值:dp[i][j] = min(dp[i][j], dp[i][k] + dp[k+1][j] + sum[j] - sum[i-1]); 最 ...

  9. Objective-C:Foundation框架-常用类-NSMutableDictionary

    直接上代码吧: #import <Foundation/Foundation.h> @interface Student : NSObject @property (nonatomic, ...

  10. NABCD模型(猫咪记单词)

    项目需求分析与建议-NABCD模型(猫咪记单词)   N (Need 需求) 对于现在的学生,尤其是大学生来说,学习英语是一件非常重要的事.我们有四级六级托福雅思等各种各样的英语方面的考试.而学习英语 ...