Boost.URL Logo

PrevUpHomeNext
url_view::collect

Return a read-only copy of the URL, with shared lifetime.

Synopsis
std::shared_ptr< url_view const >
collect() const;
Description

This function makes a copy of the storage pointed to by this, and attaches it to a new constant url_view returned in a shared pointer. The lifetime of the storage for the characters will extend for the lifetime of the shared object. This allows the new view to be copied and passed around after the original string buffer is destroyed.

Example
std::shared_ptr<url_view const> sp;
{
    std::string s( "http://example.com" );
    url_view u( s );                        // u references characters in s

    assert( u.data() == s.data() );         // same buffer

    sp = u.collect();

    assert( sp->data() != s.data() );       // different buffer
    assert( sp->string() == s);        // same contents

    // s is destroyed and thus u
    // becomes invalid, but sp remains valid.
}
std::cout << *sp; // works

PrevUpHomeNext