How to swap two variables using multiple template parameters in C++

1 Answer

0 votes
#include <iostream>

template<class T, class U>
void swap(T &a, U &b) {
    T temp = a;
    a = b;
    b = temp;
}

int main() {
    int x = 10;
    long y = 25;
    
    swap(x, y); // swap<int, long>(x, y);

    std::cout << x << " " << y;
}




/*
run:

25 10

*/

 



answered Dec 5, 2022 by avibootz

Related questions

1 answer 159 views
2 answers 185 views
1 answer 155 views
1 answer 193 views
1 answer 244 views
1 answer 243 views
...