I need to define a preprocessor macro swap(t, x, y) that will swap two arguments x and y of a given type t in C/C++.Can anyone have any opinion on how can i do it?
相关问题
- Multiple sockets for clients to connect to
- What is the best way to do a search in a large fil
- glDrawElements only draws half a quad
- Index of single bit in long integer (in C) [duplic
- Equivalent of std::pair in C
If you want to swap basic types like int or char (which implement the XOR operator) you can use the tripple XOR trick to swap the values without the need of an additional variable:
If you're swapping complex types (float, structs, ...) you need a helper variable:
Usage of those two macros is like this: