在看LiteSQL这个项目的源码的时候发现一段非常有趣的代码:
我的第一反应是:string 竟然还可以这样用?随即写了一段测试代码:
编译后却得到这样的错误:
$ g++ -o tt tt.cc
tt.cc: In function ‘int main(int, char**)’:
tt.cc:9: error: no match for ‘operator*’ in ‘std::basic_string<char, std::char_traits<char>, std::allocator<char> >(((const char*)"#"), ((const std::allocator<char>&)((const std::allocator<char>*)(& std::allocator<char>())))) * 30’
由此看来这并不是STL的标准特性,于是想起了C++的符号重载,马上使用 grep:
$ grep "operator/*" * -R
...
src/library/string.cpp:std::string operator*(int amount, const std::string &s) {
src/library/string.cpp:std::string operator*(const std::string &s, int amount) {
这下都明白了: