Solved

Passing Template to a Template function.

Posted on 2003-11-22
4
308 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to split this in C++ 4 103
wordmultiple challenge 12 136
Counting documents in a Domino View 3 85
Adding Namespace to the second node of the xml 16 73
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
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 …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

803 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