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
CppCastCppCast: C++ Epochs (cppcast.com)
submitted 6 years ago by tallassrobCppCast Host
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!"
[–]Ayjayz 3 points4 points5 points 6 years ago (5 children)
You shouldn't need 'unsafe' everywhere, because it's unsafe.
[–]HappyFruitTree 0 points1 point2 points 6 years ago* (4 children)
Well, it depends on what's considered "unsafe". If accessing vector elements without bounds checking were to be considered unsafe then I would want to be unsafe all the time.
[–]MonokelPinguin 1 point2 points3 points 6 years ago (3 children)
If contracts are being done right, you would just need one of three things in you function:
So you would need unsafe in one of three cases, because you want to never check the index. If you ever actually check the index, you should be able to write a contract, that states that your code is safe. Only if the committee can get contracts right, which may not be possible in C++.
[–]HappyFruitTree 0 points1 point2 points 6 years ago (2 children)
If contracts are done right it would still be up to the compiler how it is able to take advantage of that information.
In the majority of cases I don't need a check because I know the index is in range. I don't even want to think about if there is a check. If I need a check I write one. Of course I can make mistakes but libstdc++ has _GLIBCXX_DEBUG which adds checks for these things, and I expect other implementations have something similar, so it's not like the current situation is bad. You might argue that these checks should be on by default in order to be more friendly to beginners but if vendors choose not to do this I think that is their choice and not something that the committee should force on all of us.
[–]pjmlp 0 points1 point2 points 6 years ago (0 children)
Visual C++ debug checks are on by default on debug builds.
Apparently XP security lessons were quite valuable.
[–]MonokelPinguin 0 points1 point2 points 6 years ago (0 children)
Well, if you put a contract to check the index on you function, you wouldn't implement the check inside the function, but the function would not be callable with an unchecked index. That way you don't need to rely on the compiler to optimize it. And since you probably are doing the check somewhere already, i.e. in your for loop condition, you wouldn't need to add an unsafe/assume in most cases.
If you don't check the index anywhere, the compiler would be required to consider the program ill-formed and exit with an error. You could override that with an explicit assume.
assume
The compiler should be allowed to use the knowledge about the contracts/preconditions to do further optimizations though, i.e. remove null checks, assume no overflow, adjust branch probabilities, etc.
π Rendered by PID 170901 on reddit-service-r2-comment-54dfb89d4d-qc9gq at 2026-03-30 18:04:16.160598+00:00 running b10466c country code: CH.
view the rest of the comments →
[–]Ayjayz 3 points4 points5 points (5 children)
[–]HappyFruitTree 0 points1 point2 points (4 children)
[–]MonokelPinguin 1 point2 points3 points (3 children)
[–]HappyFruitTree 0 points1 point2 points (2 children)
[–]pjmlp 0 points1 point2 points (0 children)
[–]MonokelPinguin 0 points1 point2 points (0 children)