In which versions of the C++ standard does “(i+=10)+=10” have undefined behaviour?

tl;dr: The sequence of the modifications and reads performed in (i+=10)+=10 is well defined in both C++98 and C++11, however in C++98 this is not sufficient to make the behavior defined. In C++98 multiple modifications to the same object without an intervening sequence-point results in undefined behavior, even when the order of those modifications is … Read more

C++11-style [[unused]] attribute in gcc?

There is [[maybe_unused]] attribute in C++17. It’s implemented in GCC 7, see C++ Standards Support in GCC . Example from P0212R1 proposal: [[maybe_unused]] void f([[maybe_unused]] bool thing1, [[maybe_unused]] bool thing2) { [[maybe_unused]] bool b = thing1 && thing2; assert(b); }

Class and std::async on class member in C++

do_rand_stf is a non-static member function and thus cannot be called without a class instance (the implicit this parameter.) Luckily, std::async handles its parameters like std::bind, and bind in turn can use std::mem_fn to turn a member function pointer into a functor that takes an explicit this parameter, so all you need to do is … Read more

Is “sizeof new int;” undefined behavior?

The warning doesn’t state that it’s UB; it merely says that the context of use, namely sizeof, won’t trigger the side effects (which in case of new is allocating memory). [expr.sizeof] The sizeof operator yields the number of bytes occupied by a non-potentially-overlapping object of the type of its operand. The operand is either an … Read more

Volatile in C++11

Whether it is optimized out depends entirely on compilers and what they choose to optimize away. The C++98/03 memory model does not recognize the possibility that x could change between the setting of it and the retrieval of the value. The C++11 memory model does recognize that x could be changed. However, it doesn’t care. … Read more

Why is std::weak_ptr::expired optimized away?

Your program is incorrect; the shared-ownership pointer facilities are not intended to be used for synchronization. [intro.multithread]/24: The implementation may assume that any thread will eventually do one of the following: — terminate, — make a call to a library I/O function, — access or modify a volatile object, or — perform a synchronization operation … Read more

using c++ aggregate initialization in std::make_shared

You could create an adapter with a variadic constructor template to forward the arguments, something like: template<class T> struct aggregate_adapter : public T { template<class… Args> aggregate_adapter(Args&&… args) : T{ std::forward<Args>(args)… } {} }; And then you can do: auto foo = std::make_shared<aggregate_adapter<Foo>>(“hello”, 5, ‘c’); Since aggregate_adapter<Foo> and Foo are related, foo is convertible to … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)