471,584 Members | 1,598 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,584 software developers and data experts.

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
Jul 19 '05 #1
2 6168
go**********@elvendesigns.com (Elven) wrote in
<91*************************@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

Jul 19 '05 #2
On 10 Aug 2003 12:47:31 -0700, go**********@elvendesigns.com (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
Jul 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Dmitry Epstein | last post: by
31 posts views Thread by nikola | last post: by
8 posts views Thread by Imre | last post: by
4 posts views Thread by stinos | last post: by
5 posts views Thread by (2b|!2b)==? | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by lumer26 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.