By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,750 Members | 1,159 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,750 IT Pros & Developers. It's quick & easy.

Template member function specializations in template classes

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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.