C++ CRTP initialization
问题 i ran into a segfault running the following program #include <iostream> #include <vector> template <typename Derived> struct CRTPBase { CRTPBase() { func(); } void func() { static_cast<Derived*>(this)->_func(); } }; struct CRTPChild : CRTPBase<CRTPChild>{ using CRTPBase<CRTPChild>::CRTPBase; void _func(){ vec.resize(10); vec[0] = 2; } std::vector<int> vec; }; int main() { CRTPChild obj; std::cout << obj.vec[0] << std::endl; } When i replace vec with a member of type int it doesn't segfault