how can i make swapping for 2 variables in more than 2 ways ??

Dani AI

Generated

Good list by and nice follow-up from . The items below fill gaps the original examples didn’t cover: how to make swaps safe and optimal for user types, concise modern alternatives, and pitfalls to avoid.

For user-defined types, provide a member swap and a non-member friend swap in the same namespace so argument-dependent lookup (ADL) finds it. Mark it noexcept when possible so containers and algorithms can prefer moves instead of copies during reallocation or algorithms that depend on non-throwing moves:

namespace mylib {
struct Widget {
    void swap(Widget& other) noexcept {
        using std::swap;
        swap(a, other.a);
        swap(b, other.b);
    }
    friend void swap(Widget& x, Widget& y) noexcept { x.swap(y); }
};
}

For concise generic code with move-only types, std::exchange (since C++14) is handy; it returns the old value while assigning a new one:

template<typename T>
void swap_with_exchange(T& a, T& b) {
    a = std::exchange(b, std::move(a));
}

Notes and caveats - do not use XOR-swap except as a curiosity: it breaks with aliasing and non-integral types. For trivially copyable objects, a memmove-based block swap can be faster but is only safe for POD/trivially-copyable types. For ranges use std::iter_swap or std::swap_ranges. In multithreaded code, std::atomic<T>::exchange affects a single atomic object; swapping two independent atomics atomically needs explicit synchronization or compare-exchange loops. Overall guidance: prefer an ADL-friendly noexcept swap for custom types, use std utilities (exchange/iter_swap) for clarity, and only reach for low-level tricks after profiling and careful correctness checks.

Recommended Answers

All 3 Replies

Question is confusing.. What do you mean "make swapping"?

There are many ways to swap two variables..

std::swap(variable_a, variable_b);

//OR

template<class T> 
void do_swap(T& a, T& b) 
{ 
    T temp = std::move(a);
    a = std::move(b); 
    b = std::move(temp);
}

//OR

template<typename T>
void do_swap(T& a, T& b)
{
    T temp = b;
    b = a;
    a = temp;
}

//OR

template<typename T>
void do_swap(T* a, T* b)
{
    T temp = *b;
    *b = *a;
    *a = temp;
}

//OR

template<typename T>
void do_swap(T* a, T* b)
{
    *a ^= *b;
    *b ^= *a;
    *a ^= *b;
}

//OR

template<typename T>
void do_swap(T& a, T& b)
{
    a ^= b;
    b ^= a;
    a ^= b;
}

//OR

void do_swap(void* a, void* b, size_t size) //Usage: do_swap(&va, &vb, sizeof(va));
{
    uint8_t* c = static_cast<uint8_t*>(a);
    uint8_t* d = static_cast<uint8_t*>(b);

    int i;
    uint8_t t = 0;
    for (i = 0; i < size; ++i)
    {
        t = c[i];
        c[i] = d[i];
        d[i] = t;
    }
}

Thanks triumphost. i found that helpful to me as well.

oh! thanks so much :) ..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.