Forum Replies Created
-
AuthorPosts
-
::
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.
::According to the index operator of std::array, it makes sense.
The const reference guarantees that it cannot be modified, and constexpr that I can potentially be executed at compile time.
C++23 supports the multidimensional subscript operator:
#include <array> #include <iostream> template<typename T, std::size_t X, std::size_t Y> struct Matrix { std::array<T, X * Y> mat{}; T& operator[](std::size_t x, std::size_t y) { return mat[y * X + x]; } }; int main() { std::cout << '\n'; Matrix<int, 3, 3> mat; for (auto i : {0, 1, 2}) { for (auto j : {0, 1, 2}) mat[i, j] = (i * 3) + j; } for (auto i : {0, 1, 2}) { for (auto j : {0, 1, 2}) std::cout << mat[i, j] << " "; } std::cout << '\n'; }
::With C++20, I would strongly suggest std::atomic_flag. Before, you must use std::condition_variable. For one-time synchronization in C++11, you can also use a future/promise pair. std::condition_variable is the slowest and most complicated variant. The post “Performance Comparison of Condition Variables and Atomics in C++20” will give you the first numbers on Windows.
When you don’t break the memory order sequential consistency, atomics behave pretty natural. On X86, there is no reason for breaking the strong memory order sequential consistency.
::Very good. The program has a memory leak. Window should have a virtual destructor.
This is how the program behaves: https://godbolt.org/z/rzY4bMvr5
This is how the program should behave: https://godbolt.org/z/T3xsaebcf
-
AuthorPosts