type safe printf

在書裏看到的,摘錄以下:ios

#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;
}
相關文章
相關標籤/搜索