Forum Replies Created
-
AuthorPosts
-
::
In addition, you can link in C++23 operations on std::optional or std::expected. See: C++23: A New Way of Error Handling with std::expected
::Here is Andreas Fertig answer:
I don’t know what exactly the reason is, but I know the reasons for the second variant. The alignas only applies to the one variable at the top, foo. However, it can easily be passed to a function, for example, and the alignment is ignored: https://compiler-explorer.com/z/8K9Kds519.
The same applies to assignments to a suitably sized std::byte. This can have a different alignment.
With the storage variant, the alignment of foo is preserved, provided that the complete structure is passed to a function. I strongly suspect that this is also their motivation.
::You should use a std::vector if the container is to become larger at runtime or very large. std::array uses the stack, whereas std::vector uses the heap. The size of the stack is very limited.
With C++26 we get the std::inplace_vector. Its capacity is defined at compile time so that new elements can be added at runtime. std::inplace_vector is a drop-in replacement for std::vector.
std::vector and std::array could only be used in constant expressions.
::The term factor pattern is not as precisely defined as necessary. We should not concentrate on the terms but on the behavior.
- One difference is that unique_ptr and shared_ptr know what they want to create.
- std::shared_ptr from this is a special factory method.
- unique_ptr and shared_ptr do not support the covariant return type: Question about covariant return types
20. October 2024 at 20:57 in reply to: Class for polynomials using std::move and std::forward #636758::Non-generic case: There is no need to define the destructor, the move constructor, the move assignment operator, or copy constructor. Your compiler can auto generate them. Additionally, you forgot the copy assignment operator. https://godbolt.org/z/8rvj6csqW
Generic case: Thanks to perfect forwarding you can simplify the initialize member functions. One function is sufficient. https://godbolt.org/z/P3hndoh8K
template<typename T> void initialize(int deg, T&& t) { degree = deg; coefficients = std::forward<T>(t); }
9. October 2024 at 18:54 in reply to: Move semantics with std::unique_ptr instead of a raw pointer #636727::Honestly, your argumentation is too sophisticated for me. You don’t know, in general, how your objects are used.
Here is my rule of thumb:
- Each instance of the class requires its state; instances should be initialized lazily => local static
- Instances of the class share state => static data member
::There is an elegant way to sort a container of variants. Use in C++20 ranges and projections. The following example sorts an array of variants.
std::ranges::sort(arr, std::less(), [](auto const& x){ return std::visit([](auto const& e){ return static_cast<double>(e); }, x); });
Consider std::sort and use the parallel execution policy in C++17:
25. September 2024 at 11:39 in reply to: Basic illustration of use of const and/or reference #6366628. August 2024 at 17:12 in reply to: Why isn’t auto allowed in a definition where the declaration specifies the type #6355753. April 2024 at 11:16 in reply to: Synchronizing overriden methods in Thread Safe Interface #633222::Deriving from the Thread Safe Interface is critical. Prefer instead the NVI idiom: The Template Method.
-
AuthorPosts