3 ms·
C++ uses value type to mean either a scalar object (int, tuple<double> etc) or a container that manages heap memory for you, e.g. a vector of a value type. If y
by JonChesterfield 3y ago
C++ uses value type to mean either a scalar object (int, tuple<double> etc) or a container that manages heap memory for you, e.g. a vector of a value type. If you stay in that world you can basically ignore memory management.
- spacechild1 3y agoBut std::shared_ptr is also a value type :)
- mgaunard 3y agoIt isn't. If you copy a value and change the copy, the original is unaffected.
- spacechild1 3y agoC++ classes in general are value types. The fact that instances might share a common resource does not change that. See also https://learn.microsoft.com/en-us/cpp/cpp/value-types-modern-cpp?view=msvc-170 https://learn.microsoft.com/en-us/cpp/cpp/value-types-modern....
- dataflow 3y agoStaying away from std::unique_ptr<T> and std::unique_ptr<T[]> while using std::vector<T> sounds kind of silly. The last one is a generalized version of the first two. So claiming you don't use the first two is really misleading.
- mgaunard 3y agovector is a regular value type, unique_ptr isn't.
- dataflow 3y agoI'm not sure how you define "value type" (it certainly isn't C++ terminology; are you coming from C#?) but in any case, this is a distinction without a difference. You can replace every use of std::unique_ptr with std::vector and just switch a few method calls (like using .data() instead of .get()) and you'd achieve the same effects, just slower. I'm not sure what the point would be though, other than to be able to claim that you don't use smart pointers.