Am I right in assuming that
class D { /* ... */ };
int f (const D & t) { return /* something calculated from t */; }
template<class T>
class C {
private:
int m_i;
T m_t;
// or first m_t, then m_i -- careless order of declarations
public:
template<class T_>
C (T_ && t) : m_t (std::forward<T_> (t)), m_i (f (t)) {
}
};
C<D> c (D ());
may lead to a bug since the value of t has been moved away when f(t) is called? Is there any way to avoid this problem apart from (i) using a factory function or (ii) introducing a dependency on the order in which m_i and m_t are declared?
The first thing is that the order of evaluation of the initializer list is determined by the order of the members in the class definition, so in your case, it will be:
So in your case it is fine. It would also be fine if you reordered the members so that
m_tis declared beforem_iand you usedm_tin the initialization: