利用模板变参实现简单的printf

void Printf(const char* s)
{
    while (*s)
    {
        std::cout << *s++;
    }
}
template <typename T, typename... Types>
void Printf(const char* s, const T& firstArg,const Types&... args)
{
    while (*s)
    {
        if (*s == '%' &&  *(++s) != '%')
        {
            std::cout << firstArg;
            Printf(++s, args...);
            return;
        }
        else
        {
            std::cout << *s++;
        }
    }
}

 

posted @ 2021-02-05 13:02  ho966  阅读(129)  评论(0编辑  收藏  举报