I have a segment similar to the following.
struct derive : base{
derive(unique_ptr ptr): base{func(ptr->some_data), std::move(ptr)}{}
};
In theory, it should work. But since the compiler (vs2015) does not strictly follow the standard, the order of func(ptr->some_data), std::move(ptr)
is undefined, i.e. ptr
may be moved before accessed.
So my problem is how to make this segment work as expected?
Complete code like this:
#include <memory>
struct base {
virtual ~base() = 0 {}
protected:
base(std::unique_ptr<base> new_state) :
previous_state{ std::move(new_state) } {}
private:
std::unique_ptr<base> previous_state;
};
struct derive_base : base {
int get_a() const noexcept {
return a;
}
protected:
derive_base(int const new_a, std::unique_ptr<base> new_state) :
base{ std::move(new_state) }, a{ new_a } {}
private:
int a;
};
struct final_state : derive_base {
final_state(std::unique_ptr<base> new_state) :
derive_base{ dynamic_cast<derive_base&>(*new_state).get_a(), std::move(new_state) } {}
};