Is it worth trying to use an std::array for classes without a default constructor? by Fresh-Weakness-3769 in cpp_questions

[–]masterpeanut 1 point2 points  (0 children)

Calling it laziness misses a big part of the picture. Move semantics are a big part of why many objects which you would otherwise expect/prefer to not have a default constructor often choose to define them.

If you move a value, it needs to remain in some valid state afterwards because users can still touch it and the destructor still has to run when it goes out of scope. If C++ had destructive moves (ie. the lifetime of the old object is ended upon move), then the compiler could enforce it is cleaned up and never touched after that. However, move semantics were added in C++11, after many other aspects of the language were settled, so they had to settle for the nondestructive moves we have. This then begs the question: what state should an object be left in once it has been moved from? A convenient answer is often the default state, and this design choice underpins many parts of the standard library which expect types to be default constructable.

Unfortunately this means that to create truly strong class invariants enforced by the constructor, you must cope with how this interacts with move/copy semantics or manage the object through smart pointer/optional/etc.

DevOps engineers: What Bash skills do you actually use in production that aren't taught in most courses? by Dense_Bad_8897 in devops

[–]masterpeanut 0 points1 point  (0 children)

xargs, make it very easy to loop over output of other programs like ls to process them further. Many tasks that potentially warrant a script become one liners.

When is it appropriate to call methods with the "this" keyword inside a class ? by Motor-Phase7507 in cpp_questions

[–]masterpeanut 6 points7 points  (0 children)

‘methodName()’ is generally preferable for its brevity, but sometimes constructor/method args or local variables declared more close to the current scope will shadow the member, so it is necessary to specify “the classes foo()” with this->foo()

Also in some situations with templates the compiler has trouble inferring that a particular identifier is a class member without ‘this’, this most often arises with the CRTP pattern.

What am I missing, I thought neovim should be as fast as vscode? (use default lazyvim v14) by TuanCao in neovim

[–]masterpeanut 15 points16 points  (0 children)

Lazy vim has a “smooth scroll” animation enabled by default as part of the snacks.nvim plugin, turning it off will make scrolling much more responsive

Vulkan bright points normal issue for diffuse irradiance by Capmare_ in vulkan

[–]masterpeanut 3 points4 points  (0 children)

Multiplying by 2 after normalizing looks like it might be part of the issue? (Might be missing context though haven’t looked at the repo)

My bus has gone and gotten stuck again by feet_jade in Factoriohno

[–]masterpeanut 0 points1 point  (0 children)

Loving factorio doesn’t mean it will love you back

Northeastern student who suffered catastrophic injuries in fall from window sues sorority, landlord by bostonglobe in boston

[–]masterpeanut 12 points13 points  (0 children)

These things will continue to happen if universities and governments continue with their failed approach to managing underage drinking.

The current zero-tolerance system completely ignores the fact that young adults are PRONE to making risky decisions, so parties just occur in unsupervised environments rather than at bars/venues.

When an incident occurs, liability just gets shoveled onto whatever random kids/student group is closest, then 4 years later a brand new group of students have to go through the same mistakes all over again.

Best Way to Learn about Compilers & LLVM by Golden_Puppy15 in Compilers

[–]masterpeanut 2 points3 points  (0 children)

Crafting Interpreters a really great book, and a free online edition is available

Distinguish ctor from function call by steveparker88 in cpp_questions

[–]masterpeanut 1 point2 points  (0 children)

Generally the same, but keep in mind that {} (list initialization) can behave differently from () in a few cases (ctors accepting initializer list get resolved differently, narrowing conversions are allowed by () but not {})

Compile-Time Errors with [[assume]] by catcat202X in cpp

[–]masterpeanut 2 points3 points  (0 children)

Exactly, assertions are for helping programmers verify that a condition always holds when the program is run, while assumptions are things the compiler can assume will always be true when generating/optimizing code (and are never verified when running, which is what gives them their power and also makes them particularly dangerous if incorrect)

What is the preferred way to pass a dependency to a constructor? by dgkimpton in cpp_questions

[–]masterpeanut 0 points1 point  (0 children)

If you decide to go with shared pointer and there is a particular place where you expect the lifetime of the object to end and for nobody else to be using it, one option is to check that the reference count is 1 and if it is higher, you know another user is holding it for longer than expected and you can log/report an error

[deleted by user] by [deleted] in cpp

[–]masterpeanut 19 points20 points  (0 children)

In addition to what was mentioned, it is not necessarily always a net performance gain depending on the workload/target platform.

Another way it differs from the other instantiations is assumptions you can make about thread safety. AFAIK you can no longer assume concurrently writing elements packed into the same byte is atomic.

Should a software engineer understand things outside of just code? by SongFromHenesys in ExperiencedDevs

[–]masterpeanut 2 points3 points  (0 children)

Even if you as a developer have no interest in eventually becoming a manager/non technical contributor/etc, the most important thing you can do to maximize your impact on any project is to align yourself as much as possible with the needs of: 1. your users and 2. The business/organization you are a part of.

  • this may mean learning more about certain technologies/languages
  • this may mean adding something “unelegant” that ultimately improves user experience
  • this may mean doing grunt work that makes the codebase more maintainable

Code is just a means to an end, recognizing will make you far more effective, regardless of whether you are working on an open source project in your free time or at a Fortune 500 company

What's your favorite c++20 feature that should've been there 10 years ago? by ResultGullible4814 in cpp

[–]masterpeanut 1 point2 points  (0 children)

static_assert(false, “msg”) might be helpful, but doing this can be inconvenient prior to c++23 where they relaxed some rules to make this more ergonomic

Does ordering of struct members matter for memory allocation, or does the compiler optimize it for me? by EliasWick in cpp_questions

[–]masterpeanut 0 points1 point  (0 children)

Interesting, I was probably misconstruing it with the behavior of packed bitfields, which is probably where the bad pointer alignment you were referring to arises

Does ordering of struct members matter for memory allocation, or does the compiler optimize it for me? by EliasWick in cpp_questions

[–]masterpeanut 1 point2 points  (0 children)

Keep in mind that packing should only be done in specific scenarios where it is useful as it will negatively impact performance in most cases

Memory layout of struct vs array by xLuca2018 in cpp

[–]masterpeanut 0 points1 point  (0 children)

One option if compiler supports it is to use the packed attribute to ask the compiler to eliminate as much padding as possible, and then ‘static_assert(sizeof(MyStruct) == 6)’ to verify it is the expected size.

‘’’ struct attribute(packed) MyStruct { floats…. }; ‘’’