Is it safe to swap two different vectors in C++, using the std::vector::swap method?

Yes, this is perfectly safe to swap vectors of the same type.

Vector under the hood is just a few pointers pointing to the data the vector uses and "end" of the sequence. When you call swap you just exchange those pointers between the vectors. You don't need to worry that the vectors are the same size because of this.

Vectors of different types cannot be swapped using swap. You'd need to implement your own function that does the conversion and swapping.


It is safe because nothing is created during the swap operation. Only data members of the class std::vector are swapped.

Consider the following demonstrative program that makes it clear how objects of the class std::vector are swapped.

#include <iostream>
#include <utility>
#include <iterator>
#include <algorithm>
#include <numeric>

class A
{
public:
    explicit A( size_t n ) : ptr( new int[n]() ), n( n )
    {
        std::iota( ptr, ptr + n, 0 );   
    }

    ~A() 
    { 
        delete []ptr; 
    }

    void swap( A & a ) noexcept
    {
        std::swap( ptr, a.ptr );
        std::swap( n, a.n );
    }

    friend std::ostream & operator <<( std::ostream &os, const A &a )
    {
        std::copy( a.ptr, a.ptr + a.n, std::ostream_iterator<int>( os, " " ) );
        return os;
    }

private:    
    int *ptr;
    size_t n;
};

int main() 
{
    A a1( 10 );
    A a2( 5 );

    std::cout << a1 << '\n';
    std::cout << a2 << '\n';

    std::cout << '\n';

    a1.swap( a2 );

    std::cout << a1 << '\n';
    std::cout << a2 << '\n';

    std::cout << '\n';

    return 0;
}

The program output is

0 1 2 3 4 5 6 7 8 9 
0 1 2 3 4 

0 1 2 3 4 
0 1 2 3 4 5 6 7 8 9 

As you see only data members ptr and n are swapped in the member function swap. Neither additional resources are used.

A similar approach is used in the class std::vector.

As for this example

std::vector<Widget> WidgetVector;

std::vector<Widget2> Widget2Vector;

then there are objects of different classes. The member function swap is applied to vectors of the same type.


Is it safe to swap two different vectors in C++, using the std::vector::swap method?

Yes. Swapping can generally be considered safe. On the other hand, safety is subjective and relative and can be considered from different perspectives. As such, it is not possible to give a satisfactory answer without augmenting the question with a context, and choosing what sort of safety is being considered.

Is it still safe to swap the two vectors with the std::vector::swap method: WidgetVector.swap(Widget2Vector); or it will lead to an UB?

There will not be UB. Yes, it is still safe in the sense that the program is ill-formed.