2016-12-03 06:58:21 -08:00
|
|
|
#include <iostream>
|
2016-12-03 07:08:38 -08:00
|
|
|
#include <sstream>
|
2016-12-03 06:58:21 -08:00
|
|
|
|
|
|
|
struct Class
|
|
|
|
{
|
|
|
|
int a = 300;
|
|
|
|
};
|
|
|
|
|
|
|
|
std::ostream &operator << (std::ostream &out, Class c)
|
|
|
|
{
|
|
|
|
out << c.a;
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Print(char const *fmt)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
for(auto p = fmt; *p; p++, ret++)
|
|
|
|
std::cout.put(*p);
|
|
|
|
|
|
|
|
std::cout.put('\n');
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, typename... Args>
|
|
|
|
int Print(char const *fmt, T &&head, Args &&...tail)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
for(auto p = fmt; *p; p++, ret++)
|
|
|
|
{
|
|
|
|
if(p[0] == '{')
|
|
|
|
switch(p[1])
|
|
|
|
{
|
2016-12-03 07:08:38 -08:00
|
|
|
case '}':
|
|
|
|
if(std::ostringstream o; true)
|
|
|
|
{
|
|
|
|
o << head;
|
|
|
|
std::cout << o.str();
|
|
|
|
return (o.str().size()) + ret + Print(p + 2, tail...);
|
|
|
|
}
|
2016-12-03 06:58:21 -08:00
|
|
|
case '{': p++; break;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::cout.put(*p);
|
|
|
|
}
|
|
|
|
|
2016-12-03 07:08:38 -08:00
|
|
|
return ret;
|
2016-12-03 06:58:21 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
Class c;
|
2016-12-03 07:08:38 -08:00
|
|
|
// should output:
|
|
|
|
// a91230b{}c{d300e{}f
|
|
|
|
// ret = 19
|
|
|
|
int ret = Print("a{}b{{}c{d{}e{}f", 91230, c);
|
|
|
|
Print("ret = {}", ret);
|
2016-12-03 06:58:21 -08:00
|
|
|
}
|