Vulpo One

Tag: c++


Almost Monkey Patching in С++

include <string>

std::string operator*(std::string& s, unsigned int n)
{
    std::string cml;

    cml.reserve(s.size() * n);

    for (decltype(n) i = 0; i < n; i++) {
        cml += s;
    }

    return cml;
}

int main()
{
    std::string s   = "test";
    std::string sss = s*3;

    std::cout << sss << std::endl; // testtesttest
}

Comments