Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6572643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:01:55+00:00 2026-05-25T15:01:55+00:00

I have this class template: template<class… T> class Test { std::vector<TestCase*> test_cases; public: Test()

  • 0

I have this class template:

template<class... T>
class Test {
  std::vector<TestCase*> test_cases;
public:
  Test() {
    // Here, for each T an instance should be added to test_cases.
    test_cases.push_back((new T)...);
  }
};

This works fine for one template argument, but for multiple arguments I get this error:

error: too many arguments to function call, expected 1, have 2

How can I use variadic templates with new this way? What is the correct syntax?


EDIT: I think my question wasn’t quite clear. What I want is this:

Test<TestCase1, TestCase2, TestCase3>;
// The constructor will then be:
test_cases.push_back(new TestCase1);
test_cases.push_back(new TestCase2);
test_cases.push_back(new TestCase3);

My compiler is clang 163.7.1 with this flag: -std=c++0x.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T15:01:57+00:00Added an answer on May 25, 2026 at 3:01 pm

    vector::push_back expects one parameter so you can’t expand the variadic template in the function call.
    Also I added a template parameter for the base class (from which all other classes derive).

    Here’s something that compiles.

    struct base{};
    struct d0 : base{};
    struct d1 : base{};
    struct d2 : base{};
    
    #include <vector>
    
    // termination condition for helper function
    template <class T>
    void add(std::vector<T*>&) { 
    }
    
    // helper function
    template <class T, class Head, class... Tail>
    void add(std::vector<T*>& v) { 
           v.push_back(new Head()); 
           add<T, Tail...>(v);
    }
    
    template <class T, class ... U>
    class test
    {
        std::vector<T*> vec;
    public:
        test() {
           add<T, U...>(vec);      
        }
    };
    
    int main() 
    {
        test<base, d0,d1,d2> t;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a C++ class like this: class ConnectionBase { public: ConnectionBase(); template <class
I have code that looks like this: template<class T> class list { public: class
I have a template class which goes like this: class Template{ public $pageTitle =
Hallo I have this script: <? require(lib2/config.inc.php); require(lib2/tpl.class.php); require(lib2/db.class.php); require(lib2/um.class.php); $tpl = new template(templates,
I have non-template class with a templatized constructor. This code compiles for me. But
Suppose I have code like this: template<class T, T initial_t> class Bar { //
Assume I have a function template like this: template<class T> inline void doStuff(T* arr)
Suppose I have a function which looks like this: template <class In, class In2>
I have this class called Table: class Table { public string Name { get
I have this class. public class Foo { public Guid Id { get; set;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.