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

时间:2016-02-12 20:49:22

标签: c++ templates constructor shared-ptr variadic-functions

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 UNIQUE container. If no parameters are passed in then the very first std::vector< std::shared_ptr<T> > in the shared_ptr<> will be set and stored as a vector<>. 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

nullptr

definition

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.

};

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:

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();
}

The internal nodes private member variable would have #include "SomeHeader.h" int main() { int x = 3; int y = 5; int z = 7; Nodes<int> someNodes( x, y, z ); return 0; } where

shared_ptr<int>

and if no parameters are passed as in...

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

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.

1 个答案:

答案 0 :(得分:3)

ALTER PROCEDURE [dbo].[InsertScheduledBulkUpdateRecords] @scheduledEndDateTime DATETIME, AS BEGIN SET NOCOUNT ON; INSERT INTO ScheduledBulkUpdate (ScheduledEndDateTime) VALUES (@scheduledEndDateTime) END seems a better choice, but with variadic template, it should be something like:

initializer_list

with 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))...} {} }; :

initializer_list

Demo