Solved

Passing Template to a Template function.

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sumDigits  challenge 7 74
Magic Software info 18 125
Installed softare without GUID 6 66
Is doing tutor.com teaching in my situation advisable? 2 54
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…
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
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 …
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…

920 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

13 Experts available now in Live!

Get 1:1 Help Now