« first day (2857 days earlier)      last day (2 days later) » 

2:32 AM
How to swap two elements which belong to the same std::forward_list? I am afraid that the std::swap may also swap the pointers which belongs to the interator.
 
 
4 hours later…
nwp
6:09 AM
@John Just std::swap(element1, element2);. The location of the elements doesn't matter.
 
After the std::swap(element1, element2) has been invoked, the iterator acquired before is still valid or not?
Say std::forward_list<int> fw_lst{1,2,3};
auto itr = fw_lst.begin();
std::swap(*itr, *(itr+1));
Is the `itr` still valid?
`itr+1` is wrong in my last comment.
Here is the [example code](godbolt.org/z/cbn1Kx5vo):
#include<forward_list>
#include<iostream>

int main()
{
    std::forward_list<int> fw_lst{1,2,3};
    auto itr1 = fw_lst.begin();
    auto itr2 = std::next(itr1);
    std::swap(*itr1, *itr2);

    auto show_itr_pointed=[](decltype(itr1) itr){
        std::cout << *itr << std::endl;
    };

    show_itr_pointed(itr1);
    show_itr_pointed(itr2);
}
 
 
2 hours later…
nwp
8:26 AM
@John Yes, it's still valid. std::swap doesn't even know there was an iterator involved and the iterator doesn't keep track of value changes, it just returns a T & upon dereferencing and has no idea about the value of it.
 

« first day (2857 days earlier)      last day (2 days later) »