• Advertisement

Archived

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

can you specialize a inner templated method of a template class

This topic is 5001 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hi i''m trying to specialize a templated method of an already templated type, to do this something like this:
template< typename val, typename c >
class example {
public:

    template< typename trav_policy >
    iterator< val, trav_policy > begin();
};

template< typename val, typename c >
template< typename trav_policy > //inner template

iterator< val, trav_policy > 
example< val, c >::begin() { /*doing stuff*/ } // general types


template< typename val, typename c >
template< pre_order >           //inner template specialized

iterator< val, pre_order > 
example< val, c >::begin() { /*doing stuff*/ }   // specialization

Using GCC 3.4 it doesn''t like it, throwing errors about it cannot do the specialization of the inner template.

Share this post


Link to post
Share on other sites
Advertisement

What you are doing should be possible. Section 14.7.3 paragraph 16 in the C++ standard:

A member of member template of a class template may be explicitly specialized for a given implicit instantiation of the class template, even if the member or member template is defined in the class template definition. An explicit specialization of a member or member template is specified using the template specialization syntax.


However, your syntax looks off. Explicit template specializations of function templates use the template <> syntax with the template parameters following the function name in angle brackets. e.g.:
template <class T> template <> void A<int>::f<char>(T, char);


edit: Strike that. It looks like to specialize a member template of a template class you need to specialize the enclosing template class. (Paragraph 18 following the previous paragraph I quoted.)

[edited by - SiCrane on June 10, 2004 8:14:04 AM]

Share this post


Link to post
Share on other sites
Well, I tweaked with the code, and was able to get it to compile on VC++ 7.1. It didn''t require class template specialization, as SiCrane suggests, and I''m not sure what the standard says about it.


template< typename val, typename c >
class example {
public:

template< typename trav_policy >
iterator< val, trav_policy > begin() { } // general types


template<> //inner template specialized

iterator< val, pre_order > begin<pre_order>() { } // specialization

};



- Houdini

Share this post


Link to post
Share on other sites
If you want the implementations outside of the class definition:


template<typename val, typename c>
class example
{
public:
template<typename trav_policy> iterator<val, trav_policy> begin();
template<> iterator<val, pre_order> begin();
};
//

template<typename val, typename c>
template<typename trav_policy>
iterator<val, trav_policy> example<val, c>::begin(){
// "normal" template

}
//

template<typename val, typename c>
template<>
iterator<val, pre_order> example<val, c>::begin(){
// specialized

}

Share this post


Link to post
Share on other sites
Yes i''ve tried that first on GCC 3.4 but it doesn''t accept it, VS .NET 2003 does accept it.

Share this post


Link to post
Share on other sites
It works on Borland C++ BuilderX
Nondebugged, but compileable code:
template <typename T>
template<typename F>
inline T* LINKED_LIST<T>::findfirstwhere(F Predicate){
LL_NODE<T>* node_cur=head;
while(node_cur){
if((*Predicate)(*node_cur->data)){
return node_cur->data;
}
node_cur=node_cur->next;
}
}

Share this post


Link to post
Share on other sites

  • Advertisement