简体   繁体   中英

C++ call templated constructor for template class

I have a template class, with variadic argument list:

template<class ...Args>
struct Data{
};

Now I want to have constructor with variadic "universal reference" argument list, so I make my constructor templated:

template<class ...Args>
struct Data{

    template<class ...CtrArgs>
    Data(CtrArgs&& ... args){
        // do something
    }

};

And now I want to make an instance of Data :

Data<int, MyClass, bool> dat(1, MyClass(), false);
     ^^^^^^^^^^^^^^^^^^
     Is this Args? Or CtrArgs?

The question is, does this <int, MyClass, bool> goes to Args, or to CtrArgs?

PS Maybe this is easy to check. But I ask this because I have very strange behavior in more complex case.

Data<int, MyClass, bool> is the type obtained by instantiating the class template Data with the template arguments int , MyClass , bool . So in your example, the template arguments go to Args .

There is no way to explicitly specify template arguments for a constructor. The C++ standard even says so unequivocally (§14.8.1/7):

[ Note: Because the explicit template argument list follows the function template name, and because conversion member function templates and constructor member function templates are called without using a function name, there is no way to provide an explicit template argument list for these function templates. end note ]

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