!CE-9 0 Newbie Poster

Hello,

I'm trying to implement a system of resource managers. There's a ResourceManager template and its templated member function which will attempt to create a resource from whatever _Creator had been passed (if the given resource can use the given creator class). The _Creator is just an aggregation of information to be used, by the _Resource.

There's also a ResourceIndexer class which derives (implemented-in-terms-of) from ResourceManager. Has an additional template parameter which defines a type which will be a key type (but is also a _Creator).

In the ResourceIndexer's case, I need to apply specialization to the Manage() template member function: when a _Creator of type _Index had been passed, the Indexer has to use it for indexing. For the rest of the cases it just calls ResourceManager::Manage().

When I try the following,

template <class _Resource>
class ResourceManager
{
public:
   template <typename _Creator>
   Handle   Manage(_Creator c) { ... };
};

template <class _Resource, typename _Index>
class ResourceIndexer : protected ResourceManager<_Resource>
{
public:
   template <typename _Creator>
   Handle   Manage(_Creator c) { return ResourceManager<_Resource>::Manage(c) };

   template <>
   Handle   Manage<_Index>(_Creator c) { ... };
};

or

template <class _Resource, typename _Index>
class ResourceIndexer : protected ResourceManager<_Resource>
{
public:
   template <typename _Creator>
   Handle   Manage(_Creator c) { ... };
};

template <_Resource, _Index>
Handle   ResourceIndexer<_Resource, _Index>::Manage<_Index>(_Creator c) { ... };

or

template <_Resource, _Index>
Handle   ResourceIndexer<_Resource, _Index>::Manage<>(_Creator c) { ... };

I get different compiler errors, respectively,

/Volumes/PROJECTS/Development/manager.hpp:265: error: invalid explicit specialization before '>' token
/Volumes/PROJECTS/Development/manager.hpp:265: error: explicit specialization in non-namespace scope 'class XR::ResourceIndexer<_Resource, _Index>'
/Volumes/PROJECTS/Development/manager.hpp:265: error: enclosing class templates are not explicitly specialized
/Volumes/PROJECTS/Development/manager.hpp:266: error: invalid member function declaration

and

/Volumes/PROJECTS/Development/manager.hpp:329: error: template-id 'Manage<_Index>' in declaration of primary template
/Volumes/PROJECTS/Development/manager.hpp:329: error: prototype for 'XR::Handle XR::ResourceIndexer<_Resource, _Index>::Manage(const _Creator&, XR::Handle)' does not match any in class 'XR::ResourceIndexer<_Resource, _Index>'
/Volumes/PROJECTS/Development/manager.hpp:260: error: candidate is: template<class _Resource, class _Index> template<class _Creator> XR::Handle XR::ResourceIndexer::Manage(const _Creator&, XR::Handle)
/Volumes/PROJECTS/Development/manager.hpp:329: error: template definition of non-template 'XR::Handle XR::ResourceIndexer<_Resource, _Index>::Manage(const _Index&, XR::Handle)'

Reading around I've found that template specialization has to be defined in their enclosing namespaces, that's why I moved it outside in the second attempt -- but that way the compiler didn't seem to recognize the function as a template member function in the first place.

How is this doable? What's the correct syntax / approach?

Many thanks for your help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.