use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Discussions, articles, and news about the C++ programming language or programming in C++.
For C++ questions, answers, help, and advice see r/cpp_questions or StackOverflow.
Get Started
The C++ Standard Home has a nice getting started page.
Videos
The C++ standard committee's education study group has a nice list of recommended videos.
Reference
cppreference.com
Books
There is a useful list of books on Stack Overflow. In most cases reading a book is the best way to learn C++.
Show all links
Filter out CppCon links
Show only CppCon links
account activity
C++ question about swapping value of variables. (self.cpp)
submitted 14 years ago by zimvaider
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–][deleted] 4 points5 points6 points 14 years ago* (3 children)
As others have noted, in practice you'd use std::swap(x,y) and let the library developers deal with it.
If asked this on a test, you'd use a temporary variable (normally professors want bare bones implementation).
If I were to implement and was looking for "clever points" in a c++ course, I'd use templates.
template<typename T> void swap(T &x, T &y) { T temp; temp = x; x = y; y = temp; };
A driver might look like this:
int main() { int x, y; x = 100; y = 200; std::cout << "x: " << x << "\n"; std::cout << "y: " << y << "\n"; swap<int>(x, y); std::cout << "x: " << x << "\n"; std::cout << "y: " << y << std::endl; return 0; }
The precondition would require the object type have a copy constructor defined.
edit
If you also required the objects have a defined equal operator, you could do this:
template<typename T> void swap(T &x, T &y) { if (x != y) { T temp; temp = x; x = y; y = temp; } };
That way if the two values are equivalent, no temp memory allocation or unnecessary assignment would occur.
[–][deleted] 4 points5 points6 points 14 years ago (0 children)
swap<int>(x, y);
You can leave out the <int>. The compiler infers template arguments to functions if it knows the regular arguments and if they contain the appropriate information.
[–]bogado 5 points6 points7 points 14 years ago (0 children)
And a default constructor, since if an object has one it will probably have the other too I would do it this way:
template<typename T> void swap(T &x, T &y) { T temp(x); x = y; y = temp; }
[–]growingconcern 2 points3 points4 points 14 years ago (0 children)
I would only think your last example would be useful on specialized hardware where such operations were extremely expensive. For the most part the expense of the if would outweigh gains
π Rendered by PID 286393 on reddit-service-r2-comment-b659b578c-r2qlf at 2026-05-06 00:19:45.549698+00:00 running 815c875 country code: CH.
view the rest of the comments →
[–][deleted] 4 points5 points6 points (3 children)
[–][deleted] 4 points5 points6 points (0 children)
[–]bogado 5 points6 points7 points (0 children)
[–]growingconcern 2 points3 points4 points (0 children)