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 ...
随机推荐
- 数据库函数--nvl、coalesce、decode比较
SQL中 nvl().coalesce().decode()这三个函数nvl(bonus,0) 2个参数 if bonus is null return 0 else return bonus,ora ...
- 一些简单css3效果的整理
代码: html: <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...
- js一些题目
假期在家,看到的,昨天看了下: 原文链接:http://wwwcqamin.duapp.com/?p=102#comment-7 请说出下面程序的输出结果 第一题: 1 2 3 4 5 6 7 8 9 ...
- <mvc:annotation-driven />注解意义
<mvc:annotation-driven /> 是一种简写形式,完全可以手动配置替代这种简写形式,简写形式可以让初学都快速应用默认配置方案. <mvc:annotation-dr ...
- ASP.NET服务器控件数据绑定总结
using System; using System.Collections.Generic; using System.Text; using System.Web.UI.WebControls;/ ...
- 冲销交货单WS_REVERSE_GOODS_ISSUE
LOOP AT ITAB. AT END OF VBELN. PERFORM FRM_LOCK_DELIVERY(ZSDS0002) USING ITAB-VBELN. CALL FUNCTION ' ...
- 预处器的对比——Sass、LESS.
发挥CSS预处器的作用是一种很有挑战性的事情.CSS预处器有不同的语言,有不同的语法和功能. 不同CSS预处器的蛮量.功能以及他们的好处——Sass.LESS 介绍 CSS预处理器是一种语言,用来编写 ...
- PHP超级全局变量——Session 变量
PHP session 变量用于存储有关用户会话的信息,或更改用户会话的设置.Session 变量保存的信息是单一用户的,并且可供应用程序中的所有页面使用. PHP Session 变量 当您运行一个 ...
- qml操作播放器
现在增加了一个filter属性,所以可以很好和opencv结合.转一篇文章(http://blog.qt.io/blog/2015/03/20/introducing-video-filters-in ...
- 用户列表-投资记录sql
--普通标.定向标.新手标.老互融计划-投资记录表select bid.borrow_id, (select yyb.borrow_valid_time from YYD_Borrow_BorrowI ...