避免临时对象的字符串加法

之前我有一篇文章《 C++ 中让对象的拷贝成为 显式 的》,使用类似的技巧,可以避免字符串加法中的临时对象,也许是因为惰性,这个想法一直没有诉诸实现,今天有空把它写了出来。

先看看这段代码:

std::string a = "A";
std::string b = a + "B" + "C" + "D" + "E" + "F";

在C++11之前的标准 C++1998/2003 中,为了计算 b,这段代码一共创建了 5 个临时 std::string 对象。

实现代码非常简单:

#include <string>
template<class String>
class strjoin_helper {
private:
    String v;
    typedef typename String::value_type char_t;
    typedef strjoin_helper me;
public:
    template<class StrX> explicit strjoin_helper(const StrX& x) : v(x) {}
    template<class Char> explicit strjoin_helper(const Char* s, ptrdiff_t n) : v(s, n) {}
    operator String() const { return v; }
    me& operator+(const String& y) { v += y; return *this; }
    me& operator+(const char_t* y) { v += y; return *this; }
    me& operator+(const me& y) { v += y.v; return *this; }
    friend me operator+(const char_t* x, const me& y) { me t(x); t.v += y.v; return t; }
    friend me operator+(const String& x, const me& y) { me t(x); t.v += y.v; return t; }
};

template<class AnyString>
strjoin_helper<AnyString> strjoin(const AnyString& x) { return strjoin_helper<AnyString>(x); }

strjoin_helper<std::string> strjoin(const char* s) { return strjoin_helper<std::string>(s); }
strjoin_helper<std::string> strjoin(const char* s, ptrdiff_t n) { return strjoin_helper<std::string>(s, n); }

strjoin_helper<std::wstring> strjoin(const wchar_t* s) { return strjoin_helper<std::wstring>(s); }
strjoin_helper<std::wstring> strjoin(const wchar_t* s, ptrdiff_t n) { return strjoin_helper<std::wstring>(s, n); }


点击这里查看完整代码


使用的时候更简单:

using namespace std;

int main() {
    string a = "123_" + strjoin("") + "A" + "B" + strjoin("abc") + strjoin("012345", 4);
    cout << a << endl;
    return 0;
}


用一个简单的程序测一下性能,这个程序的输出结果:

loop=1000000: fast[temp_cnt=2000002 time=0.715966] slow[temp_cnt=10000002 time=1.305356]

总结

strjoin 使用了两个 C++1998 中鲜为人知的特性:

  1. 如 《C++ 中让对象的拷贝成为 显式 的》中所说,临时对象可以被修改,但不能绑定到非常量引用上
  2. 临时对象的生存期为整个表达式,而不是某个真子表达式



posted on 2012-12-06 14:04  能发波  阅读(211)  评论(0编辑  收藏  举报

导航