Archived

This topic is now archived and is closed to further replies.

Grizwald

Templated Factory Function

Recommended Posts

Grizwald    270
I have a class, called CEngine. This class is NOT templated, but i have a method in it i want templated called AllocateEngineObject. here is the function prototype:
    
template <typename T> 
  T *AllocateEngineObject();
  
here is the implementation:
        
template <typename T>
  T *CEngine::AllocateEngineObject()
  {
	T *temp = new T(this);
	EngineObjects.push_back (temp);
 	EngineObjectCount++;
 	return T;
  }
  
  
now this compiles without error. But when the function is used in this way:
        
Driver = AllocateEngineObject<CWindowsDriver>(); 
  
EngineObjects is an STL vector of type IEngineObject. CWindowsDriver is derived from IEngineObject so it would be a legal downcast, right? Driver is a pointer to CWindowsDriver. i get these errors: error C2275: 'CWindowsDriver' : illegal use of this type as an expression error C2059: syntax error : ')' if you need me to clear anything up, please ask. Thanks for any help. -Cody [edited by - Grizwald on October 21, 2002 9:38:11 PM]

Share this post


Link to post
Share on other sites
kuphryn    210
There is an error in the return function. You return a type T. You should return temp.

template T *CEngine::AllocateEngineObject()
{
T *temp = new T(this);
EngineObjects.push_back (temp);
EngineObjectCount++;
return T; }

It is not possible to partialize template in function templates. The class must be templatized.

Kuphryn

[edited by - kuphryn on October 21, 2002 10:04:15 PM]

Share this post


Link to post
Share on other sites
Grizwald    270
thanks for the replies. So it is absolutely impossible to have a member-function template when the class isn''t templatized? that would suck if it is! If it is, how could i go about doing the same thing without templatizing the class...thanks again

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
http://www.dofactory.com/patterns/pattern_factory.asp

Share this post


Link to post
Share on other sites
Shannon Barber    1681
quote:
Original post by Grizwald
thanks for the replies. So it is absolutely impossible to have a member-function template when the class isn''t templatized? that would suck if it is! If it is, how could i go about doing the same thing without templatizing the class...thanks again


You can do that, you just aren''t allowed to partially-specialize them - which you''re not. The only error that you are attemping to return T, which is a type (like int or float) not a pointer to one.

Share this post


Link to post
Share on other sites
Nypyren    12065
The poor compiler thought you meant:

Driver = AllocateEngineObject [IsLessThan] CWindowsDriver [IsGreaterThan] ();

Because the template meaning of angle brackets are not part of the grammar for expressions unless you're using dynamic_cast or one of those types of keywords.

[edited by - Nypyren on October 22, 2002 10:24:59 PM]

Share this post


Link to post
Share on other sites
risingdragon3    382
No, no, it will work that way. the use of the angle brackets is allowed in that situation.
And the solution has already been posted. Change the code you wrote for the factory function to return temp, not T.

Share this post


Link to post
Share on other sites
daerid    354
Nypyren, can I get some of what you''re smoking? Templates have absolutely nothing to do with RTTI, casting, or inheritence.

Share this post


Link to post
Share on other sites
the Speed Bump    240
heh.

You have to define the function in every sourcefile it''s used in; you can''t just prototype in the header and define in the matching sourcefile like nontemplate functions, since the compiler needs to be able to recompile it for every type passed.

The most common practice is to just define the function in the header. (a compiler supporting export sure would be nice...)

Share this post


Link to post
Share on other sites
Shannon Barber    1681
Comeau supports export (to some extent) because it uses EDG's compiler.

export is a C++ disaster (eclipsed only by the crappy exception specification semantics) and ought to be deprecated in C++0x.

It is inherently infeasible to semantically implement the desired feature to the full-extent in a compiled & linked build process. It amounts to nothing short of clairsentience or the effective elimination of object files - they would necessarily become like .Net assemblies, requiring them to fully describe all type information contained there-in (which includes function/method signatures and potentially definitions.) The end result is an obj that is as-good-as having the original source code, which defeats the entire purpose.

It's possible to partially implement export subjecting it to various limitations and inefficiencies that template code is proposed to solve, e.g. inlining predicate code.

Because it was a pipe-dream tossed into the C++ spec for some god-forsaken-reason, many areas of the semantic behavior are left unspecified. In short, export will never be implemented the same way any two compilers (if it's implemented at all), so you won't ever be able to rely on it.


[edited by - Magmai Kai Holmlor on October 23, 2002 11:42:15 PM]

Share this post


Link to post
Share on other sites