std::inplace_vector<T,N>::try_push_back

Conditionally appends the given element value to the end of the container.

# Declarations

constexpr pointer try_push_back( const T& value );

(since C++26)

constexpr pointer try_push_back( T&& value );

(since C++26)

# Parameters

# Return value

std::addressof(back()) if size() < capacity(), nullptr otherwise.

# Notes

This section is incompleteReason: Explain the purpose of this API.

# Example

#include <cassert>
#include <inplace_vector>
#include <string>
 
int main()
{
    std::inplace_vector<std::string, 2> pets;
    std::string dog{"dog"};
 
    std::string* p1 = pets.try_push_back("cat"); // overload (1)
    assert(*p1 == "cat" and pets.size() == 1);
 
    std::string* p2 = pets.try_push_back(std::move(dog)); // overload (2)
    assert(*p2 == "dog" and pets.size() == 2);
 
    assert(pets[0] == "cat" and pets[1] == "dog");
    assert(pets.size() == pets.capacity());
 
    std::string* p3 = pets.try_push_back("bug");
    assert(p3 == nullptr and pets.size() == 2);
}

# See also