Skip to main content
gamedev.net
🔒 Locked

Function Template Specialization

Started by Tang of the Mountain Mar 21, 2009 at 12:04 AM 4 replies 1.1k views
Original Post
Tang of the Mountain
Tang of the Mountain
(It's late, I thought I clicked in general programming, my apologies) Ok, I have this cluster of inherited objects. Base A derives from base B,C,D derive from A Classes B, C, D's ctors have the same params. A is the outcast with one extra param. I am developing a templated object manager which will create any of the given types. The problem is the specialization needed for A. If I do the following inside the manager: Well, I can't. Because its not really a specialization of a template. Its a new function. But I want the functionality of this function to work solely for A. I don't want to have to overload Create and always return NULL then create a specialization of the overloaded Create for type A to work normally. However, that's what I'm thinking. Any ideas? I could change the ctor of A but I'm trying to avoid that for now.
We have youth, how about a fountain of smart.e4 e5 f4 d5
Deyja
Deyja
The first attempt is not a specialization of the template, it is an entirely new set of templates/specializations. You'll need to create a template for it to specialize first, perhaps one that fails to compile so it will only work with A.

Granted that extra parameter entirely defeats the purpose of using templates at all..
Metsan
Metsan
Also, this thread may be of interest to you, as several approaches for the design and implementation of generic factories are referenced and/or discussed in it.

[Edited by - Metsan on March 21, 2009 6:18:34 AM]
loufoque
loufoque
If I understand what you're saying, you want to overload a function, but make that overload only visible if the template argument is A.

This is exactly what SFINAE does.
Antheus
Antheus
Would this work:
Base* Create(const std::string & name, int id = 0)...} {}



Quote:
Any ideas? I could change the ctor of A but I'm trying to avoid that for now.

This is generally a better idea.

But instead of changing constructor, consider changing the way they are used. If there's one exception, there's likely to be more later on.

When constructing objects from a factory, I prefer to pass configuration parameters to it. Something like:
Base * Create( std::map<std::string, std::string> &params);


Then, each instance queries the map for parameters it needs. This supports future extensions and customizations without breaking the API. Gotcha: parameter pairs need to be documented in a visible place, or using such code becomes cumbersome. It's also better to derive parameter class for each class to provide named functions for setting the values.
Zahlman
Zahlman
1) Is it really appropriate for client code to Create A instances directly instead of a derived instance?

2) Instead of specializing the template, you might just override it?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.