473,407 Members | 2,326 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 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 6247
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jeff | last post by:
/* -------------------------------------------------------------------------- Hello, I was experimenting with class templates and specializing member functions and came across a simple problem...
6
by: Dmitry Epstein | last post by:
Here is an example of a problem, which I tried to reduce to its bare essentials: // begin test1.cpp class E { public: template<class T> static void f(); }; template<class T> void E::f() {}
31
by: nikola | last post by:
Hi all, I was working with a simple function template to find the min of two values. But since I would like the two values to be different (type) I dont know what kind of value (type) it will...
13
by: Walt Karas | last post by:
The following gives an error in the declaration of the member function x() of the class template Tpl, compiliing with a recent version of GCC under Solaris: class A { }; class B { }; ...
1
by: Imre | last post by:
Let's suppose we have a primary template with one argument defined in a header file. Two source files include this header, and both define a specialization of the primary template. Later, both...
8
by: Imre | last post by:
Hi I'm looking for a way to make sure that whenever a new instance of a class template A is created, then an instance of class template B is also created, with the same template parameters. Of...
4
by: stinos | last post by:
Hi All! suppose a class having a function for outputting data somehow, class X { template< class tType > void Output( const tType& arg ) { //default ToString handles integers/doubles
8
by: Rahul | last post by:
Hi, Is there a way to partially specialize only a member function of a template class (not the whole class). e.g. template <typename A, typename B> class Base { public:
5
by: (2b|!2b)==? | last post by:
I would like to know if I can specialize only a specific method for a class template. Is the (specialization) code below valid? template <typename T1, typename T2> class MyClass { public:
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.