Solved

Passing Template to a Template function.

Posted on 2003-11-22
4
320 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
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…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

749 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