C++ CRTP initialization

前端 未结 3 943
有刺的猬
有刺的猬 2020-12-22 02:05

i ran into a segfault running the following program

#include 
#include 

template 
struct CRTPBase {
           


        
相关标签:
3条回答
  • 2020-12-22 02:11

    The base-class will be initialized (i.e. constructed) before the child class. That means when you call CRTPChild::_func the CRTPChild part of the object (including the vector) haven't been constructed yet. Using the vector in any way will lead to undefined behavior.

    Don't access (non-static) members of child-classes in a base-class constructor.

    0 讨论(0)
  • 2020-12-22 02:14

    When CRTPBase constructor is called, CRTPChild is not yet fully constructed, so calling it's member function is undefined behavior.

    The way undefined behavior manifests itself depends on platform, compiler and phase of the moon.

    In particular, when your member is an int, the fact that it is not yet constructed doesn't cause program to crash when you are using int - there are no invariants for int. Vector, on the other hand, has invariants, so accessing unconstructed vector will violate them, and cause incorrect memory access.

    0 讨论(0)
  • 2020-12-22 02:27

    Your code has undefined behavior. The problem comes from the order of the initialization; for the derived class CRTPChild, the constructor of the base class CRTPBase<CRTPChild> is invoked firstly, after that the data member vec of CRTPChild get initialized. When _func is invoked (from the constructor of the base class) vec is not initialized at all.

    2) Then, direct base classes are initialized in left-to-right order as they appear in this class's base-specifier list

    3) Then, non-static data members are initialized in order of declaration in the class definition.

    Changing the type to int it's still UB. UB means anything is possible, it might lead to segfault or might not.

    0 讨论(0)
提交回复
热议问题