Comment by MORPHOICES

Comment by MORPHOICES 16 hours ago

5 replies

Value categories and move semantics are great examples of programming concepts that can cause confusion, and it's a great example of how not having a bad documentation can still lead to confusion through bad mental models. ~

Intuitively you think you understand what is going on, and you think you can answer what is going on, and you can even use it due to understanding it on an operational level, but you can't explain it due to your confusion.

As a result, you most likely are going to create a lot of small bugs in your software and a lot of code that you don't really understand. So, I'm curious to know what others think.

What concept did you learn later than you thought you would? What knowledge did you struggle with the most? What finally helped you understand it?

porise 12 hours ago

Value categories actually just are confusing in a language as complicated as C++. I'm not willing to bet that even senior C++ developers are always going to be able to deduce the correct value category.

And worse, in typical C++ fashion, there is still little guaranteed as far as when std::move will actually cause a move. The implementation is still given a lot of leeway. I've been surprised before and you basically have no choice but to check the assembly and hope it continues to be compiled that way as minor changes make their way into the code base.

  • nickelpro 5 hours ago

    > even senior C++ developers are always going to be able to deduce the correct value category

    Depends what "senior" means in this context. Someone with 20-years of domain experience in utility billing, who happened to be writing C++ for those 20 years? Probably not.

    Someone who has been studying and teaching C++ for 20 years? Yes they are able to tell you the value category at a glance.

    Language experience is not something you develop accidentally, you don't slip into just because you're using the language. Such tacit experience quickly plateaus. If you make the language itself the object of study, you will quickly surpass "mere" practitioners.

    This is true of most popular programming languages in my experience. I find very, very few Python programmers understand the language at an implementation level, can explain the iterator protocol or what `@coroutine` actually used to do, how `__slots__` works, etc.

    C++ is not unique in this, although it is old and has had a lot more time to develop strange corners.

HarHarVeryFunny 11 hours ago

The issue TFA is describing isn't really about not understanding move semantics, it's about not having read the documentation for the STL container classes, and not therefore realizing that anything requiring reallocation needs a noexcept move constructor (else will fall back to copy construction).

Note that a move constructor that is NOT declared with noexcept is perfectly valid, and will happily be used most of the time (other than where code, such as the STL, is explicitly looking for a noexcept one).

So, for example:

HeavyObject t;

HeavyObject s(std::move(t));

Will cause t to be moved to s.

meindnoch 14 hours ago

Coming from other languages with generics, it took a while for me to internalize SFINAE when writing templated code.

  • spacechild1 12 hours ago

    Luckily, with C++17's if-constexpr and C++20's concepts, SFINAE has become mostly obsolete for new C++ code (unless you have/want to support older C++ standards).