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 ...
随机推荐
- eclipse导入git项目(转)
1.首先在github.com上申请一个账号2.Eclipse需要安装egit插件,在Eclipse中选择help->Marketplace,在search中输入egit,找到后安装即可 3.从 ...
- cssText
cssText 的本质就是设置 HTML 元素的 style 属性值 cssText 的方便之处在于一次可以写很多属性,而且变更 CSS 样式不必变 JS 代码,只需变样式字符串.但它有个缺点,就是它 ...
- (13)odoo翻译
-------------------更新时间:15:52 2016-09-28 星期三 增加模型名翻译17:26 2016-05-20 星期五17:58 2016-05-17 星期二12:14 20 ...
- Sui 弹框固定
SUI是一套基于bootstrap开发的前端组件库,同时它也是一套设计规范,可以非常方便的设计和实现精美的页面,是一个简单易用.功能强大的UI库.自己在使用sui过程之中,总是忘记它的一些Api,今天 ...
- php solr 查询
$options = array( 'hostname' => 'localhost', 'port' => 8080, 'path' => 'solr/test'); $clien ...
- [apache]用shell分析网站的访问情况
随着网站正式运行,我们可以通过通用的免费日志分析工具比如awstats获得一些实际访问网站的信息,例如每天ip量,pv量,用户所用的的浏览器,用户所用的操作系统等,但是有时候希望通过手工方式从WEB日 ...
- SQL逻辑读变成零
使用缓存HINT 让逻辑读变成0. create table t as select * from dba_objects; insert into t select * from t; commit ...
- TextBoxButton控件的开发实现
效果图: 实现代码: public TextBoxButton() { _button = new Button { ForeColor = System.Drawing.SystemColors.G ...
- SqlServer2008快照隔离模式的业务应用
场景: 有200个检测点,每个检测点每天采集5个数据,对表的读写都是随机的(即有可能同时读写),总共有5年的数据. 存储方案A: 日期 点号 类型 值 20120101 001 A 1.0 20120 ...
- 3D MAX在立方体的使用
3D MAX不会“复用”立方体的顶点-----它直接计算该立方体需要12个三角面,每个三角面需要3个顶点,这样一共是36个顶点-----其实有大量顶点的位置是相同的,但3D MAX不管这些.它认为 ...