Boost logo

Boost :

Subject: [boost] [utility] string_ref construction from rvalue reference to string
From: Андрей Давыдов (andrey.a.davydov_at_[hidden])
Date: 2014-07-31 03:53:47


Hello, boost.

Are the any reasonable cases when construction boost::string_ref from
std::string && is desirable?
I mean that usage string_ref in the following examples is totally wrong,
but not for compiler:

    using std::string;
    using boost::string_ref;

    // 1
    {
        auto ref = string_ref(string("abacaba") + string("upyachka"));
        std::cout << ref << std::endl;
    }
    // 2
    {
        string very_long(1000000, 'a');
        auto ref = string_ref(very_long + "b");
        std::cout << ref << std::endl;
    }

In practice it gets worse, because the first example example works (gcc
4.9.1) thanks to optimizations with short literals, I suspect, and the
second example leads to segmentation fault, as expected.

Those errors can be caught at compile time if class "basic_string_ref"
defintion would contain lines like following:

template<typename Allocator>
basic_string_ref(std::basic_string<charT, traits, Allocator> &&) = delete;

I cannot understant does it prevent any correct usage of string_ref?

-- 
Andrey Davydov

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk