简体   繁体   中英

Class template with variadic constructor; storing parameters into std::vector< std::shared_ptr<> >

I'm trying to design a class template where its constructor has a variadic constructor and each parameter type must be the same data type. I would then like to store all parameters into a std::vector< std::shared_ptr<T> > container. If no parameters are passed in then the very first shared_ptr<> in the vector<> will be set and stored as a nullptr . Also I would like to be able to keep track of how many parameters are passed in. So far this is what I have tried.

declaration

template<typename T>
class Nodes {
private:
    unsigned m_numParams;
    std::vector<sstd::shared_ptr<T>> m_vNodes;

public:
    explicit Nodes( T... );  // Not sure how to set first parameter as being default to nullptr along with the variadic syntax.

};

definition

template<typename T>
Nodes<T>::Nodes( T... ) {

    if ( /* no parameters passed in or 1st parameter is nullptr */ ) {
        T* ptr = nullptr;
        m_vNodes.push_back( ptr );
    } else {
        for each ( /* parameter with same data type */ ) {
            std::shared_ptr<T> sptr( new T() );
            m_vNodes.push_back( sptr );
        }
    }
    m_numParams = m_vNodes.size();
}

My understanding of basic templates is decent, but my use of variadic functions or templates are limited and I'm trying to learn new methods to further improve my overall skill set within the c++ language. I would like to know if my initial approach is logically the correct path; if this concept is possible to achieve and if so, what would be the most efficient way to implement this so when I use this class template this is what I would be expecting to and to actually have happen as in this example case below:

#include "SomeHeader.h"

int main() {

    int x = 3;
    int y = 5;
    int z = 7;

    Nodes<int> someNodes( x, y, z );

    return 0;
}

The internal nodes private member variable would have shared_ptr<int> where

Nodes::m_vNodes[0] = address of x with value 3 stored
Nodes::m_vNodes[1] = address of y with value 5 stored
Nodes::m_vNodes[2] = address of z with value 7 stored

and if no parameters are passed as in...

#include "SomeHeader.h"

int main() {

    Nodes<int> someNodes();

    return 0;
}

the internal member variable would have a ''nullptr` stored in the vector[0] location. Would I need to also implement its default constructor Nodes(); Or is this achievable all from a single constructor?

Once I have this constructor properly working then I would have no problem moving forward to write the functions or methods to add, remove, retrieve, store, arrange or sort the data, compare the data, or manipulate the data. I've been searching here and there and haven't seem to find anything relevant to what I'm looking for. Any tips, advice, help, links or examples would be truly appreciated and I kindly thank you in advance.

initializer_list seems a better choice, but with variadic template, it should be something like:

template<typename T>
class Nodes {
private:
    unsigned m_numParams;
    std::vector<std::shared_ptr<T>> m_vNodes;

public:
    Nodes() : m_numParams(0), m_vNodes{nullptr} {}

    template <typename ... Ts>
    explicit Nodes( Ts&&...ts) :
        m_numParams(sizeof...(Ts)),
        m_vNodes{std::make_shared<T>(std::forward<Ts>(ts))...}
    {}
};

with initializer_list :

explicit Nodes(std::initializer_list<T> ini) :
    m_numParams(ini.size())
{
    for (auto&& e : ini) {
        m_vNodes.push_back(std::make_shared<T>(e));
    }
}

Demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM