Solved

Passing Template to a Template function.

Posted on 2003-11-22
4
292 Views
Last Modified: 2010-04-17
I have defined a template class in following way,

template <class T>
class Vars
{
public:
static int x;
};

class vars0{};
class vars1{};
class vars1{};

typedef Vars<vars0> temp0;
typedef Vars<vars1> temp1;
typedef Vars<vars2> temp2;

int temp0::x =0;
int temp1::x =1;
int temp2::x =2;

I am trying to write a function that will copy the static member variables from one instance of above template class to another

I have tried this&#8230;

template<Vars<typename> class B, Vars<typename> class C>
void Copy()
{
B::x = C::x;
}

&#8230;but is not working.

How can above functionality be implmented. I would also like to insure that the typename passed to this template function should be of type Vars<class T> (though T can be any type)
0
Comment
Question by:shuklasunil
  • 3
4 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 9803541
template <class T>
class Vars
{
public:
static int x;
};

class vars0{};
class vars1{};
class vars2{};

typedef Vars<vars0> temp0;
typedef Vars<vars1> temp1;
typedef Vars<vars2> temp2;


template<class B, class C>
void Copy()
{
    B::x = C::x;
}

int temp0::x = 0;
int temp1::x = 1;
int temp2::x = 2;


int main(int argc, char* argv[])
{
    Copy<temp2, temp0>();
    cout << temp2::x << endl;

    return 0;
}
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 9803553
You don't need here template template parameters since parameters of Copy function are ordinal classes. temp2 and temp0 in this case are ordinal C++ classes which are instantiations of Vars template.
Trying to pass classes without static x member to Copy template will not compiled.
0
 
LVL 48

Accepted Solution

by:
AlexFM earned 125 total points
ID: 9803652
By the way, you can read about template template parameters here:
http://www.codeproject.com/cpp/template2003.asp

But not all C++ compilers support this feature.
0
 
LVL 4

Expert Comment

by:n_fortynine
ID: 9804878
>>I would also like to insure that the typename passed to this template function should be of type Vars<class T> (though
>>T can be any type)

What you want is technically NOT possible, because the function itself must know about the name(s) in the scope, that is whether it(they) refer(s) to a name (a template) or a type. ...Unless you want to specialize your function template for all possible/desired cases of Vars<T>. Probably not what you're asking for. :)

>>How can above functionality be implmented?

I haven't tried this myself, but I would make class Vars an inheritance of a non-template class, i.e.
template <typename T> class Vars : public VarsBase { ... };
then allow you function to take variables of this type VarsBase.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now