【发布时间】:2010-09-23 00:35:17
【问题描述】:
FastFormat 库的工作方式如下:
string example;
fastformat::fmt(example, "I am asking {0} question on {1}", 1, "stackoverflow");
它还声称“100% 类型安全”。我可以理解其他库(例如boost::format)是如何通过重载operator% 来实现这一点的,我也经常使用我的代码来实现这一点。
但是,如果我能够使用逗号代替它,其他程序员就不会那么惊讶了。我真的很想知道如何在没有模板化运算符重载技巧的情况下保证类型安全。
旁注:如果您想知道“模板化运算符重载技巧”是什么,这就是 boost::format 的工作原理(主要):
struct Test
{
template<class T>
Test& operator%(const T& what) { cout << what << "\n" /* Example */; return *this; }
};
Test() % 5 % "abc";
【问题讨论】:
-
有趣的是,当我运行时我得到“5abcabc”(VS2010)
标签: c++ formatting fastformat