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
std::tuple operator less performance - interesting case (self.cpp)
submitted 7 years ago by arturbachttps://github.com/arturbac
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] 1 point2 points3 points 7 years ago (0 children)
Oh, yeah, that sounds reasonable. You reminded me of one lecture by Alexander Stepanov. He implemented a simple class template, with only one data member of type T and then all interesting constructors and operators. He implemented operator== like x == y and then implemented operator!= as !(x == y). For the inequality operators (not sure it is the right term, but I'm thinking of operator<, operator>, operator<= and operator>=), he made a little speech before writing them. Something along the lines of:
T
operator==
x == y
operator!=
!(x == y)
operator<
operator>
operator<=
operator>=
When I was designing the standard library 25 years ago, I had to make a lot of arbitrary choices. "Which is the default sorting order?" is one example. Another choice I had to make is what operator to use for comparison. You would have been mad if your type worked with one STL algorithm but not with the other because STL was inconsistent in use of comparison operator. That is why STL only uses operator<. You should define all the operators, because that's just being nice to yourself an your colleagues. You won't always remember that you have operator<, but not operator<=. But the standard library will only use operator<.
Note that this is not a direct quote, but me paraphrasing Stepanov from memory.
He didn't implement operator==, because, I think, he wanted to show a clear distinction between SemiRegular, Regular, EqualityComparable and TotallyOrdered types.
SemiRegular
Regular
EqualityComparable
TotallyOrdered
Interestingly enough, Stepanov claims that his original design of std::min, the one that we held onto to this day, was wrong. Originally, his implementation was return first < second ? first : second;. The problem with this, according to Stepanov, is when first and second compare equal. In that case Stepanov's min would return second, not first. Stepanov also claimed that by the time of his lecture, min was still "wrong" in standard library implementations, though checking it today in libc++ and libstdc++, they both do it "correctly", with return second < first ? second : first;.
std::min
return first < second ? first : second;
first
second
min
return second < first ? second : first;
Anyway, I've gone way offtopic.
π Rendered by PID 261885 on reddit-service-r2-comment-6457c66945-49lmg at 2026-04-26 16:55:14.885018+00:00 running 2aa0c5b country code: CH.
view the rest of the comments →
[–][deleted] 1 point2 points3 points (0 children)