Which one(s) of the following std::vector's member functions has thepossibility/authority to reduce

Discussion in 'C++' started by goodbyeera, Mar 10, 2014.

  1. goodbyeera

    goodbyeera Guest

    Which one(s) of the following std::vector's member functions has the possibility/authority to reduce a vector's capacity?

    I have 5 member functions in question:
    template <class T, class Allocator = allocator<T> >
    class vector {
    public:
    vector<T,Allocator>& operator=(const vector<T,Allocator>& x);
    vector<T,Allocator>& operator=(vector<T,Allocator>&& x);
    vector& operator=(initializer_list<T>);
    void assign(initializer_list<T>);
    void clear() noexcept;
    };

    Notes:
    - For assignments: Unlike vector::swap() which explicitly specifies exchanging both contents and capacity, I can't find reliable answers for the various forms of assignments.
    - For std::assign(), there are 2 other overloads:
    template <class InputIterator> void assign(InputIterator first, InputIterator last);
    void assign(size_type n, const T& u);
    And they are both explicitly defined in terms of calls to erase() and insert(), so it's guaranteed with no reduce in capacity. But for the overload taking an initializer_list as the argument, I can't find a reliable answer.
    - For std::clear(), in C++03, it's explicitly defined in terms of erase(begin(),end()), so it's guaranteed with no reduce in capacity. I can't find equivalent definition in C++11 anymore.

    Relevant excerpts from the standard are highly appreciated.

    Thanks,
    Goodbyeera
     
    goodbyeera, Mar 10, 2014
    #1
    1. Advertisements

  2. goodbyeera

    goodbyeera Guest

    Just found in table 100 of the standard that a.assign(il) is defined as a.assign(il.begin(), il.end()), where il designates an object of type initializer_list<value_type>, so std::assign() should be removed from my question list. Sorry for the overlook.

    Thanks,
    Goodbyeera
     
    goodbyeera, Mar 10, 2014
    #2
    1. Advertisements

  3. goodbyeera

    Öö Tiib Guest

    It can reduce capacity of target object and implementations do.
    It can (and does) reduce capacity of both target and x if it wants to.
    I'm incapable to find any relevant legaleze. Those lists are new,
    better safe than sorry.
    It can't reduce capacity of target but may increase it. New too,
    so better to be careful.
    Capacity of target object may not change by standard but there are some
    known old defective implementations that may change it.
    Search engines can likely find those from some previous discussions.
     
    Öö Tiib, Mar 10, 2014
    #3
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.