Category : c++

According to shared_ptr/atomic, we know that shared_ptr::reset() is thread-safe. To prevent the object being accessed by other threads from being released. I create a store ptr to preserve the expired object. So, the following codes are thread safe in any of time? class data_info { shared_ptr<sample> using; shared_ptr<sample> store; } // function1 and function2 are ..

Read more

#include <iostream> using namespace std; struct Fraction { int numerator; int denominator; }; int simplify(int numerator, int denominator) { for (int i = denominator * numerator; i > 1; i–) { if ((denominator % i == 0) && (numerator % i == 0)) { denominator /= i; numerator /= i; } } } int output ..

Read more

First, I’ll start with what does work: struct A { int a, b; }; struct S { A A_;// Member can be defined elsewhere. struct B { int c, d; };// Member can be defined locally. B B_; struct C { int e, f; } C_;// Member can be instanced on the same line… C ..

Read more

I found that there has a different execution order between two similar statements(the only difference is the below one has a more ‘;’). The destructor order is different. Does C++ has corresponding specification about that or it’s only a unspecified behavior? Environment: GCC10 #include <iostream> template <int num> struct S { S() {std::cout << "S(" ..

Read more