Hello, I've just recently tried to make a class template whose constructor should be able to accept a variable amount of arguments of the class passed to the template. I have something like this, with the prototype inside the class definition, and the actual implementation of the constructor defined outside:

template<class T>
class List
{
public:
    List(const T&, ...);
}


List::List(const T& first, ...) {
    // constructor here
}

This of course gives me errors, and I'm not entirely sure how to go about allowing the variable length of parameters, and combine that with the template idea. Thanks for any help!

Recommended Answers

All 7 Replies

1) Unless your compiler supports the keyword export,
you have to provided the template header and its definition
in the same file

2) Here is a example :

#include <iostream>
#include <ctime>
using namespace std; 

template<typename Type>
class Example
{
private:
	 Type var;
public:
	Example(Type T); //see definition below
	//inline definition
	void set(Type t) { var = t; }
	void show();
};

//Need explicit definition
//unless definied inside class
template<typename Type>
Example<Type>::Example(Type T)
{
	cout<<typeid(*this).name()<<" created\n";

	var = T;
}
template<typename Type>
void Example<Type>::show()
{
	cout<<typeid(*this).name()<<" contains : "<<var<<endl;
}
int main()
{
	Example<float> f(2);
	Example<char> c('2');
	
	cout<<endl;
	f.show();
	cout<<endl;
	c.show();

   return 0;
}

you cant have an undefined amount of variables in function deceleration. you must declare what the function returns and all parameters the function takes. if you want to send a few or alot of one type of thing i would try using an array of some sort like

void foo(int[], size);

this way the person could send to the function however many ints or whatever you want to pass.

you cant have an undefined amount of variables in function deceleration.

Seriously? There's no options that do this for regular functions, the same way it can be done for the main function when it's handed command-line arguments? I thought it was possible to do:

// something like
myFunction(12, 33, 5);

// ...rather than
int x[] = {12, 33, 5};
myFunction(x, 3);
// like you suggested

I know other languages have this functionality, like Python's *args and **kwargs options. I can always fall back to your approach, but I thought it would be much easier to use any sort of variable argument passing if C++ could offer it.

And thanks firstPerson for the help with getting the template stuff done correctly.

>Seriously? There's no options that do this for regular functions, the
>same way it can be done for the main function when it's handed
>command-line arguments? I thought it was possible to do

C++ supports function overloading in which you can have variable number or arguments:

int fold(int x,int f=2)
{
  return x*f;
}
.
.
.
std::cout<<fold(2)<<std::endl<<fold(2,3);
// prints 
// 4
// 6

C ( and thus C++ due to backward compatibility) supports variable number of argument in a function by including stdargs.h (read http://www.codersource.net/c++_variable_argument_functions.html)

Thank you siddhant3s.

Wiki

and another link LOL

Yeah, I already Googled around but for some reason combining both the template part and the variadic part got me confused. Thanks though, I'm straightening it all out!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.