假定以下代码,这是sprintf的极小替代。 (_itoa等仅用于使代码简短。)
#include <cstdlib>
#include <string>
class Arg {
public:
Arg(const std::string& s) :m_str(s) {}
Arg(const char* s) : m_str(s) {}
Arg(int digi, double number) {char buf[128]; m_str = _gcvt(number, digi, buf);}
operator const std::string& ()const { return m_str; }
private:
std::string m_str;
};
class Format {
public:
Format(/*const char* format, */std::initializer_list<Arg> args); // see below
const std::string& str()const { return m_str; }
private:
std::string m_str;
};
Format::Format(/*const char* format, */std::initializer_list<Arg> args) {
auto arg = args.begin();
auto format = std::string(*arg++);
for(const char* c = format.c_str(); *c!='\0'; ++c) {
if(*c=='%') { m_str+=*arg++; }
else { m_str+=*c; }
}
}
int main() {
std::string test1 = Format{"test Double:% String:%", {5, 456.78}, "foo"}.str();
// I want to make this work. See the braces.
std::string test2 = Format("test Double:% String:%", {5, 456.78}, "foo").str();
return 0;
}
[您知道,我想传递参数,限于类型为“ Arg”,但使用一个使用例如可变模板而不是initializer_list <>以获得更好的可读性。
我尝试过:
template<typename... T>
Format(T&& ... args) : Format(std::forward<Args>(args)...) {}
但是我得到:
error C2440: '<function-style-cast>': cannot convert from 'initializer list' to 'Format'
note: No constructor could take the source type, or constructor overload resolution was ambiguous
std::initializer_list
转到构造函数。