The following code seems to work correctly on Clang++ and GCC:
#include <vector>
class A {
private:
int i;
std::vector<A> children;
public:
A& add();
};
A& A::add() { children.emplace_back(); return children.back(); }
int main() {
A a;
A& a2 = a.add();
}
When the data member std::vector<A> is declared, A is still an incomplete type. Same when using std::vector<B> and B was only forward declared with class B;.
It should work with std::vector since it only contains a pointer-to-A.
Is this guaranteed to work, or undefined behavior?
This is undefined behavior in C++14 and earlier; well-defined in C++17 (if it's 17).
[res.on.functions]/p2, bullet 2.7:
In C++14 and earlier,
std::vectordoes not "specifically allow" this. So the behavior is undefined.For C++17, N4510, adopted at the committee's May 2015 meeting, relaxes this rule for
vector,list, andforward_list.