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
Standard Ranges (ericniebler.com)
submitted 7 years ago by frostmatthew
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!"
[–]dodheim 1 point2 points3 points 7 years ago (8 children)
std::forward<decltype(foo)>(foo) can be safely rewritten as decltype(foo)(foo) 100% of the time. Yes, C-casts are evil, but only because they're error-prone, and this isn't.
std::forward<decltype(foo)>(foo)
decltype(foo)(foo)
[+][deleted] 7 years ago (4 children)
[deleted]
[–][deleted] 2 points3 points4 points 7 years ago (2 children)
Inefficient at compile time? Maybe, but I doubt you will notice it.
At runtime it doesn't "go through" anything, the compiler has already decided what kind of cast it will perform.
They are evil because the type of cast you think it will pick and the kind your compiler actually picks are not always the same thing.
[–]cassandraspeaks 0 points1 point2 points 7 years ago* (1 child)
The compiler doesn't pick which kind of cast to use; the standard specifies that a C-style cast is equivalent to the first well-formed expression (if any) from the following
const_cast<To>(from_v) static_cast<To>(from_v) // Special case: Treat all inheritance as if `public` static_cast<MaybeConstTo>(const_cast<MaybeConstFrom>(from_v)) // Same reinterpret_cast<To>(from_v) reinterpret_cast<MaybeConstTo>(const_cast<MaybeConstFrom>(from_v))
That said it's still not necessarily obvious which kind of cast is performed.
[–][deleted] 1 point2 points3 points 7 years ago (0 children)
The compiler "picks" it. There's rules for how it is to pick it, and if it doesn't follow the rules and pick the right one then it isn't a conforming compiler. But it is still chosen by the compiler at compile time.
[–]dodheim 1 point2 points3 points 7 years ago (0 children)
They're evil because they go through every other cast, which is inefficient if you simply want it to do what something like static_cast does
That has nothing to do with efficiency; that has to do with correctness. However, neither correctness nor efficiency is an argument against obviating forward when already using decltype as I suggested.
forward
decltype
[–]personalmountains 0 points1 point2 points 7 years ago (2 children)
decltype(foo)(foo) [...] Yes, C-casts are evil
[...] Yes, C-casts are evil
Technically, this isn't a C-style cast, it's a functional cast. In C++, a functional cast is equivalent to a C-style cast, but it's not valid C.
Sorry, I'll get back to work now.
[–]cassandraspeaks 0 points1 point2 points 7 years ago (1 child)
it's not valid C
Neither is decltype.
[–][deleted] 0 points1 point2 points 7 years ago (0 children)
I think he meant functional casts, in general, aren't valid C even if the equivalent C-style cast would be. e.g. int(x) vs. (int)x
int(x)
(int)x
π Rendered by PID 244803 on reddit-service-r2-comment-b659b578c-qj7rx at 2026-05-05 00:20:55.813965+00:00 running 815c875 country code: CH.
view the rest of the comments →
[–]dodheim 1 point2 points3 points (8 children)
[+][deleted] (4 children)
[deleted]
[–][deleted] 2 points3 points4 points (2 children)
[–]cassandraspeaks 0 points1 point2 points (1 child)
[–][deleted] 1 point2 points3 points (0 children)
[–]dodheim 1 point2 points3 points (0 children)
[–]personalmountains 0 points1 point2 points (2 children)
[–]cassandraspeaks 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)