Template member function specializations in template classes



 DEVELOP > c-Plus-Plus > Template member function specializations in template classes

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Elven"
Date: 10 Aug 2003 02:47:31 PM
Object: Template member function specializations in template classes
Hi!
I was trying to find the solution to this problem, but I don't think I
could quite come up with the correct keywords to find it, since I'm
pretty sure it's been asked before. In short, here's the situation
(ignore missing namespaces, etc, since I'm not cut-pasting this..)
// a.h
template <class B>
class A
{
...
public:
template <typename Argument>
inline A & operator<<(Argument arg);
...
};
#include "a.inl"
// a.inl
template <class B>
template <typename Argument>
inline A<B> & A<B>::operator<<(Argument arg)
{
...
}
template <class B>
template <>
inline A<B> & A<B>::operator<<(Control ctl)
{
...
}
// End
Now, the problem is that my compiler (G++32) is giving me an error
'enclosing class templates are not explicitly specialized.' Now, the
question is, obviously, is there something wrong with my syntax (does
the specialization need to come first, etc), or should I get another
compiler? I've tried a couple of tricks and there're obvious
workarounds but I'd rather do it this way and in any case I'd feel
better knowing the correct syntax if such exists. Help appreciated.
E
.

User: "Grzegorz Sakrejda"

Title: Re: Template member function specializations in template classes 10 Aug 2003 03:30:19 PM
(Elven) wrote in
<913b4483.0308101147.93a2e85@posting.google.com>:

Hi!

I was trying to find the solution to this problem, but I don't think I
could quite come up with the correct keywords to find it, since I'm
pretty sure it's been asked before. In short, here's the situation
(ignore missing namespaces, etc, since I'm not cut-pasting this..)

// a.h

template <class B>
class A
{
...
public:
template <typename Argument>
inline A & operator<<(Argument arg);

inline is redundant here.

...
};

#include "a.inl"


// a.inl

template <class B>
template <typename Argument>
inline A<B> & A<B>::operator<<(Argument arg)
{
...
}

template <class B>
template <>

template <> is misleading here.

inline A<B> & A<B>::operator<<(Control ctl)
{
...
}

A<B>& A<B>::operator <<(Control ctl) is not
specialization of member function template, it is a member function
of template class A<B> .
It needs to be declared in the template class as a member function.


// End

Now, the problem is that my compiler (G++32) is giving me an error
'enclosing class templates are not explicitly specialized.' Now, the
question is, obviously, is there something wrong with my syntax (does
the specialization need to come first, etc), or should I get another
compiler? I've tried a couple of tricks and there're obvious
workarounds but I'd rather do it this way and in any case I'd feel
better knowing the correct syntax if such exists. Help appreciated.

E

I have hard time to match the error reported to the cause but maybe
it means exactly it.

Hope it helps.
grzegorz
.

User: "tom_usenet"

Title: Re: Template member function specializations in template classes 11 Aug 2003 08:23:28 AM
On 10 Aug 2003 12:47:31 -0700,
(Elven)
wrote:

Hi!

I was trying to find the solution to this problem, but I don't think I
could quite come up with the correct keywords to find it, since I'm
pretty sure it's been asked before. In short, here's the situation
(ignore missing namespaces, etc, since I'm not cut-pasting this..)

// a.h

template <class B>
class A
{
...
public:
template <typename Argument>
inline A & operator<<(Argument arg);
...
};

#include "a.inl"


// a.inl

template <class B>
template <typename Argument>
inline A<B> & A<B>::operator<<(Argument arg)
{
...
}

template <class B>
template <>
inline A<B> & A<B>::operator<<(Control ctl)

That should be:
template <class B>
template <>
inline A<B> & A<B>::operator<< <Control>(Control ctl)
but you can't specialize a member without fully specializing the
enclosing template, since that would give the compiler some confusion
over which specialization to use. e.g. add
template <>
template <class Argument>
inline A<char>& A<char>::operator<<(Argument arg)
Now what does the compiler use for A<char>::operator<<(Control)?

Now, the problem is that my compiler (G++32) is giving me an error
'enclosing class templates are not explicitly specialized.' Now, the
question is, obviously, is there something wrong with my syntax (does
the specialization need to come first, etc), or should I get another
compiler? I've tried a couple of tricks and there're obvious
workarounds but I'd rather do it this way and in any case I'd feel
better knowing the correct syntax if such exists. Help appreciated.

The way to do it is this, using a non-member and partial
specialization:
template <class B, typename Argument>
struct stream_impl
{
static void impl(A<B>& a, Argument const& arg)
{
//default impl
}
};
//non-member operator.
template<class B, typename Argument>
inline A<B>& operator<<(A<B>& a, Argument const& arg)
{
impl(a, arg);
return a;
}
Then you can specialise using:
template <class B>
struct stream_impl<B, Control>
{
static void impl(A<B>& a, Control const& arg)
{
//special impl
}
};
Tom
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER