Syntax for template objects as function parameters... 
Author Message
 Syntax for template objects as function parameters...

Quote:

> I have a template object (specifically, it looks like this):
>     CList < key *, key * >KeyList;

> Later, I want to pass that template as a parameter to a function.  What is
> the correct syntax for that?  I've never needed to do it before, and I'm
> afraid I can't quite figure it out.

Dann, I'm going to be a bit nitpicky about terminology
here because I think part of the problem is that you
are not adequately separating concepts and enough
terminology has arisen to help with that task.

Your above defined object named KeyList is an
instance of a plain class.  It is not a "template
object" in common parlance.  That plain class
happens to be an instance of a templated class.

Quote:
> Lazy tricks like making a static instance won't work, since the code must be
> reentrant.

I may be missing something here because I do
not understand how a static instance matters.

Quote:
> So later on when, I want to call a function which accepts an object of that
> type, how do I do so?  Also, what is the proper declaration of the function?

// First, a class template analogous to CList
template< class T1, class T2> class TemplatedClass {
 public:
  void ListAll();

Quote:
};

// A plain function that takes a reference to an instance
// of a class which happens to be a particular instantiation
// of the above class template.
void DoWithIntPtrs( TemplatedClass<int *, int *> & tcii )
{
  tcii.ListAll();

Quote:
}

// A function template taking a reference to an instance
// of a class which can be any instantiation of the above
// class template.
template< class T1, class T2> void DoWithWhatever( TemplatedClass<T1,T2> & tcxx )
{
  tcxx.ListAll();

Quote:
}

// Calls to above two functions
// Note that the objects named TCII and TCLL are auto.
void silly()
{
  TemplatedClass<int *, int *> TCII;
  DoWithIntPtrs( TCII );

  TemplatedClass<long *, long *> TCLL;
  DoWithWhatever( TCLL );

Quote:
}

--
Larry Brasfield
Above opinions may be mine alone.



Fri, 28 Jun 2002 03:00:00 GMT  
 Syntax for template objects as function parameters...

Quote:
>working (cough -- using typedef -- [blush squared]) but your solution is

What's wrong with a typedef?
I use them with templates all the time, because I like to keep my code
readable. Typing "std::map< std::string, SomeWeirdClass, std::less<
std::string > >" in full every time is not one of my favorites.
I don't use them for fundamental types though (which would indeed only
complicate your code), neither are they known to the global namespace (just
in classes, functions or CPP files).

Sytse.



Sat, 29 Jun 2002 03:00:00 GMT  
 Syntax for template objects as function parameters...

Quote:

>I have a template object (specifically, it looks like this):
>    CList < key *, key * >KeyList;

>Later, I want to pass that template as a parameter to a function. What is
>the correct syntax for that?

I would say you have an object, and you want to pass that object to a
function.

Quote:
>I've never needed to do it before, and I'm
>afraid I can't quite figure it out.

>Lazy tricks like making a static instance won't work, since the code must be
>reentrant.

>So later on when, I want to call a function which accepts an object of that
>type, how do I do so?  Also, what is the proper declaration of the function?

>*Blush*
>My ignorance is showing...

The template-id CList<key*,key*> is a class-name, and you use it like
any other class name, e.g.

 void fun(CList<key*,key*>& x);

--
Doug Harrison

Visual C++ MVP



Sat, 29 Jun 2002 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. VC7 does NOT support template-template parameters involving non-type parameters

2. problems with new function template syntax

3. Syntax for pointer to instance of function template???

4. problems with new function template syntax

5. problems with new function template syntax

6. Template functions as function objects

7. syntax of template method in template class

8. Syntax for declaring a template class as a friend of a non-template class

9. BUG: pointer to __fastcall function as template parameter

10. A function as template parameter

11. using structs as member function template parameter

12. Member function callbacks as template parameters

 

 
Powered by phpBB® Forum Software