473,748 Members | 9,599 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<<(Argu ment 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 6281
go**********@el vendesigns.com (Elven) wrote in
<91************ *************@p osting.google.c om>:
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<<(Argu ment 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**********@el vendesigns.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<<(Argu ment 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>(Contr ol 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>::operat or<<(Argument arg)

Now what does the compiler use for A<char>::operat or<<(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
5777
by: Jeff | last post by:
/* -------------------------------------------------------------------------- Hello, I was experimenting with class templates and specializing member functions and came across a simple problem which I can't figure out. I have a simple class C with 2 template member objects, and a function print() that prints the value of these objects. I defined a specialization of print() for C<string, char> which is called correctly. Then I wanted...
6
2193
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
3526
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 return. I tried to write something like this: template <class Type1, class Type2, class Type3> Type3 findMin(Type1 x, Type2 y){ return (x < y) ? x : y;
13
2824
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 { }; template <typename Base> class Tpl : protected Base {
1
1573
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 sources reference the template, using the same template argument, that matches both specializations. Now, in theory, both sources reference the same template with the same argument, but due to the different specializations in the sources, the exact...
8
1362
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 course, I could do it by referencing B<Tfrom A<T>, but I'd like to do it without modifying A at all. I'm afraid it's not possile with separate translation units, but it might still worth asking.
4
3355
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
2437
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
2381
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
9541
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9370
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9247
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8242
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6796
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4874
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3312
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2782
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2215
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.