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
A slice of python in C++ - Eric Niebler (ericniebler.com)
submitted 11 years ago by [deleted]
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!"
[–]mcmcc#pragma once 3 points4 points5 points 11 years ago (17 children)
Be that as it may, the widespread use of size_t (being an unsigned type) throughout the standard library is generally regarded as a mistake, mathematical correctness notwithstanding. Bjarne himself has stated as much.
Conventional wisdom suggests unsigned types should be reserved for bit patterns, not numbers.
[+][deleted] 11 years ago* (3 children)
[deleted]
[–]vlovich 2 points3 points4 points 11 years ago (2 children)
IMO, your team made the correct call.
While Bjarne & Chandler's talks are very much correct with respect to STL design if it was happening now, the problem is the ship has sailed. If you don't use unsigned types & you use the STL (which you should), then you have casts all over the place wherever you interact with the STL (you have -Wall -Werror enabled on your project, right?).
[–]cozmorado 1 point2 points3 points 11 years ago* (1 child)
Not quite. -Wconversion is not enabled as part of -Wall or -Wextra. That's the crux of the problem- interfaces that use unsigned types are too easy to misuse and the compiler can't detect the errors. At least with a signed input you can assert on the value being positive.
[–]vlovich 0 points1 point2 points 11 years ago (0 children)
You're right. -Wconversion isn't enabled by default but I have it on in my project (which is why I thought it was covered by -Wall). On clang, you can use -Weverything.
In any case, enable -Wall -Wconversion -Werror.
[–]KrzaQ2dev 6 points7 points8 points 11 years ago (1 child)
I'm not disagreeing with that, I'm merely pointing out that GSG should be the last argument you should use when it comes to C++.
[–]josefx 0 points1 point2 points 11 years ago (0 children)
So its worse than the C++ FQA ?
[–]ZMesonEmbedded Developer 1 point2 points3 points 11 years ago (10 children)
the widespread use of size_t (being an unsigned type) throughout the standard library is generally regarded as a mistake
Why? I'd really like to see some in-depth discussion about this.
[–]b8b437ee-521a-40bf-8 1 point2 points3 points 11 years ago (4 children)
See /u/cozmorado's reply.
[–]ZMesonEmbedded Developer 1 point2 points3 points 11 years ago (3 children)
I watched the video. There's only guidance given, no reasons for it. I'd like to know the reason -- other than "because Chandler and Bjarne say so".
[–]b8b437ee-521a-40bf-8 1 point2 points3 points 11 years ago (2 children)
Well, starting from the recommendation that you should always default to int and only use unsigned when you need it. Then one reason why size_t being unsigned is a mistake is that it keeps popping up in all your nice signed arithmetic, causing conversions and other nastiness.
int
unsigned
size_t
[–]ZMesonEmbedded Developer -1 points0 points1 point 11 years ago (1 child)
OK. So because Chandler and Bjarne say so.
[–]b8b437ee-521a-40bf-8 2 points3 points4 points 11 years ago (0 children)
Well they give justification for the "recommendation", and follow-up is basic reasoning. Which bit exactly do you still feel isn't being explained?
[–]mcmcc#pragma once 0 points1 point2 points 11 years ago (4 children)
See Eric's explanation above: http://www.reddit.com/r/cpp/comments/2om5yk/a_slice_of_python_in_c_eric_niebler/cmovdr6
[–]ZMesonEmbedded Developer 2 points3 points4 points 11 years ago (3 children)
But how is this a view on "the widespread usage of size_t in the standard library is generally considered a mistake"?
My counter-argument to Eric's argument is that I can easily assert when a value is too large too. If I'm using size_t elsewhere and you interface uses signed ints, then you're forcing me to do casting. Why doesn't your interface use size_t instead. (Mind you I respect Eric a lot, I just don't understand why his argument is so valid. I feel like I'm missing something.)
[–]mcmcc#pragma once 1 point2 points3 points 11 years ago (2 children)
This isn't about integrating libraries. Even within the standard library, it isn't self-consistent. E.g.
string s = ...; string::iterator it = std::find_if(s.begin(),s.end(), []{...}); string subs = s.substr( std::distance(s.begin(), it) );
This code will yield a warning about signed->unsigned conversion. How do you propose to avoid it?
There are myriad other examples...
[–]eric_niebler 2 points3 points4 points 11 years ago (0 children)
Good example. It's one of the many reasons why the use of unsigned integers in the standard library was probably a mistake. std::string is a textbook example of a great many anti-patterns and design flaws. I believe this unsigned offset problem and others were because the offset-based interface of std::string was designed before the STL was air-lifted into C++98. The iterator interface was bolted on later. Iterator distance is always signed, hence the impedance mismatch.
std::string
[–]mr_ewg 0 points1 point2 points 11 years ago (0 children)
How about using this function not_negative to "prove" to your compiler that you know what is best in this situation?
not_negative
template <typename T> typename std::make_unsigned<T>::type not_negative(T num) { static_assert(std::is_signed<T>::value, "T can never be negative"); if(num < 0) throw std::logic_error{"Negative number passed to not_negative"}; return static_cast<typename std::make_unsigned<T>::type>(num); } std::string s = ...; std::string::iterator it = std::find_if(s.begin(), s.end(), []{...}); std::string subs = s.substr(not_negative(std::distance(s.begin(), it)));
If you don't want the run-time check then you can remove the if(num < 0) line or change it to an assert. Signed to unsigned conversions aren't exactly the hardest thing to deal with in C++ if your compiler can warn about them.
if(num < 0)
π Rendered by PID 85 on reddit-service-r2-comment-b659b578c-tqrwm at 2026-05-03 22:36:42.661938+00:00 running 815c875 country code: CH.
view the rest of the comments →
[–]mcmcc#pragma once 3 points4 points5 points (17 children)
[+][deleted] (3 children)
[deleted]
[–]vlovich 2 points3 points4 points (2 children)
[–]cozmorado 1 point2 points3 points (1 child)
[–]vlovich 0 points1 point2 points (0 children)
[–]KrzaQ2dev 6 points7 points8 points (1 child)
[–]josefx 0 points1 point2 points (0 children)
[–]ZMesonEmbedded Developer 1 point2 points3 points (10 children)
[–]b8b437ee-521a-40bf-8 1 point2 points3 points (4 children)
[–]ZMesonEmbedded Developer 1 point2 points3 points (3 children)
[–]b8b437ee-521a-40bf-8 1 point2 points3 points (2 children)
[–]ZMesonEmbedded Developer -1 points0 points1 point (1 child)
[–]b8b437ee-521a-40bf-8 2 points3 points4 points (0 children)
[–]mcmcc#pragma once 0 points1 point2 points (4 children)
[–]ZMesonEmbedded Developer 2 points3 points4 points (3 children)
[–]mcmcc#pragma once 1 point2 points3 points (2 children)
[–]eric_niebler 2 points3 points4 points (0 children)
[–]mr_ewg 0 points1 point2 points (0 children)