1

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?

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

6
  • Data<int, MyClass, bool> is Args. CtrArgs is then deduced from the constructor parameters as int, MyClass, bool. Commented Jul 24, 2014 at 3:31
  • There is no way to explicitly specify template arguments to the constructor. The CtrArgs can only be deduced from the arguments given to the constructor. Commented Jul 24, 2014 at 3:31
  • @T.C. not as int&&, MyClass&&, bool&& ? Commented Jul 24, 2014 at 3:33
  • 1
    @tower120 No, the && is added in your function argument list. The deduced types themselves are reference-less. Commented Jul 24, 2014 at 3:35
  • 1
    @tower120 If you pass an rvalue. If you pass an lvalue the type will be deduced as T&, i.e., an lvalue reference, then reference collapsing turns T& && into T&. Commented Jul 24, 2014 at 3:40

1 Answer 1

2

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 ]

Sign up to request clarification or add additional context in comments.

2 Comments

I want CtrArgs to be ALWAYS auto-deducted. Is there any "special rules", in which template arguments will go to CtrArgs?
@tower120 It will always be automatically deduced if possible. All function templates behave in that way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.