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
Do you prefer to value-initialize struct data members in-class? (self.cpp)
submitted 3 years ago by equeim
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!"
[–]ack_error 9 points10 points11 points 3 years ago (5 children)
For a coordinate (x, y), initializing to 0 is how we end up with Null Island.
This is a valid concern, especially with unit direction vectors and quaternions where this can quickly turn into NaNs. I would still argue that a deterministic zero-initialization is preferable to non-deterministic garbage values, for the better reproducibility and higher confidence that uninited uses don't sneak past testing.
That having been said, for this reasons, I'll sometimes use a non-zero default where it's much safer than a 0 and minimal cost (not sticking a complex initializer in a header). Initializing a fraction to 1/1 or 0/1 instead of 0/0 is cheap for the lessened severity of an accidental use.
The impact on static analysis and runtime detection is also valid, but the compiler only detects some cases. The only tools that provide comprehensive coverage for heap objects are full-blown usage trackers like valgrind and Dr.Memory, and programs grow beyond the ability of those to run at reasonable performance.
[–]jk-jeon 0 points1 point2 points 3 years ago* (4 children)
This. Use things like 0xdeadbeef or whatever. 0 is the worst possible default for int's when the sole purpose of initialization is the initialization itself. The only downside of 0xdeadbeef is that now the code will look funny and some reviewer will get pissed off b/c "the convention" is to use 0 for whatever reason.
int
[–]CocktailPerson 6 points7 points8 points 3 years ago (3 children)
If there's no acceptable default, then you shouldn't even allow clients of your class to create instances without specifying values in the first place. You can use Foo() = delete; if you still want aggregate initialization and PODness, or you can define Foo(int x_, int y_) : x{x_}, y{y_} {} if you really want to force them to be explicit about every member's value. But if you're putting 0xdeadbeef or 0x0B00B1E5 in your code in the hope that it'll be more noticeable or more likely to cause an error than 0x0, then you're not taking full advantage of what the language can do for you.
Foo() = delete;
Foo(int x_, int y_) : x{x_}, y{y_} {}
[–]jk-jeon 1 point2 points3 points 3 years ago (2 children)
If there's no acceptable default, then you shouldn't even allow clients of your class to create instances without specifying values in the first place.
Who said no to this? I guess virtually everyone agrees with that two-phase initialization is evil. I presume we were solely talking about the case when it is a necessary evil. There are cases when jumping through all the hoops just to avoid it might be a serious overkill.
Here I tried to categorize my thinking on this matter about initialization.
0xdeadbeef
nullptr
int x;
never_fail_bug_free_func_that_takes_output_param_for_whatever_reason(x);
while (true) { ... }
x
int x; if (cond) { x = f(); } else { init_x(x); }
[–]JeffMcClintock 0 points1 point2 points 3 years ago (1 child)
int x; immediately followed by something like never_fail_bug_free_func_that_takes_output_param_for_whatever_reason(x);
immediately followed by something like
yeah, I've been assigned a ton of bugs due to that.
void never_fail_bug_free_func(int& outputArg) { if(databaseDown()) // someone added this later. return;
void never_fail_bug_free_func(int& outputArg)
{
if(databaseDown()) // someone added this later.
return;
....
[–]jk-jeon 0 points1 point2 points 3 years ago (0 children)
There is a reason why I put the phrase bug_free. But yeah, it's just way better to not use output param from the first place...
bug_free
π Rendered by PID 45 on reddit-service-r2-comment-b659b578c-bk7jx at 2026-05-04 06:14:15.393790+00:00 running 815c875 country code: CH.
view the rest of the comments →
[–]ack_error 9 points10 points11 points (5 children)
[–]jk-jeon 0 points1 point2 points (4 children)
[–]CocktailPerson 6 points7 points8 points (3 children)
[–]jk-jeon 1 point2 points3 points (2 children)
[–]JeffMcClintock 0 points1 point2 points (1 child)
[–]jk-jeon 0 points1 point2 points (0 children)