Home > front end >  how do I replace base constructor call
how do I replace base constructor call

Time:12-23

In recent SO answer, part of the snippet I'm unable to understand whats happening,

struct VariableDepthList : std::variant<std::vector<VariableDepthList>, int> {
private:
    using base = std::variant<std::vector<VariableDepthList>, int>;
public:
    using base::base;
    VariableDepthList(std::initializer_list<VariableDepthList> v) : base(v) {}
};

base(v) is calling ctor of base class, if so what is equivalent to without having using base::base?

CodePudding user response:

what is equivalent to without having using base::base?

You replace it with what base is an alias for:

struct VariableDepthList : std::variant<std::vector<VariableDepthList>, int> {
private:
public:
    VariableDepthList(std::initializer_list<VariableDepthList> v) :
        std::variant<std::vector<VariableDepthList>, int>(v) // was "base(v)"
   {}
};

Note though that without using the base class constructors, they will not be accessible to users of VariableDepthList.

If you do want the base class constructors to be available, without creating the alias base:

struct VariableDepthList : std::variant<std::vector<VariableDepthList>, int> {
private:
public:
    using std::variant<std::vector<VariableDepthList>, int>::variant;
    
    VariableDepthList(std::initializer_list<VariableDepthList> v) :
        std::variant<std::vector<VariableDepthList>, int>(v) // was "base(v)"
   {}
};
  • Related