#include <stdio.h>
#include <iostream>
#include <vector>
#include <string>
#include <cassert>

template<typename T> std::string t()     { return "?"; }
template<> std::string t<const char *>() { return "s"; }
template<> std::string t<char>()         { return "c"; }
template<> std::string t<int>()          { return "d"; }
template<> std::string t<unsigned>()     { return "u"; }
template<> std::string t<float>()        { return "f"; }
template<> std::string t<double>()       { return "lf"; }

template<typename T>
std::vector<std::string> operator+(std::vector<std::string> chs, T val)
{
    chs.push_back(t<T>());
    return chs;
}

template<typename... T>
std::vector<std::string> ty2ch(T... vals)
{
    return (std::vector<std::string>{} + ... + vals);
}

std::vector<std::string> format2ch(const char* str)
{
    std::vector<std::string> ret;

    while (*str != '\0')
    {
        if (*str == '%')
        {
            if (*(str + 1) != '\0' && *(str + 2) != '\0' && *(str + 1) == 'l' && *(str + 2) == 'f')
            {
                ret.push_back("lf");
                goto b;
            }
            if (*(str + 1) != '\0')
            {
                ret.push_back(std::string{*(str+1)});
                goto b;
            }
        }
b:
        ++str;
    }
    return ret;
}

template<typename... T>
int Tprintf(const char* format, T... vals)
{
    auto ch_format = format2ch(format);
    auto ch_vals = ty2ch(vals...);
    assert(ch_format == ch_vals);

    return printf(format, vals...);
}

int main(int argc, char *argv[])
{
    Tprintf("this is a string %s", "hahah");

    return 0;
}

 

相关文章:

  • 2021-12-25
  • 2021-09-17
  • 2021-10-24
  • 2021-06-25
  • 2021-06-16
  • 2020-12-29
  • 2021-07-18
猜你喜欢
  • 2021-06-08
  • 2021-11-21
  • 2021-09-14
  • 2022-02-08
  • 2018-06-27
  • 2019-05-15
相关资源
相似解决方案