Template class as template parameter 
Author Message
 Template class as template parameter

Hello,

given a template like that:

template<class T> class C1{
public:
        int f(){
                return 0;
        };

Quote:
};

and say, I define

int C1<int>::f(){
        return 3;

Quote:
}

everything is fine, but defining  instead

template<class X> basic{
//...

Quote:
}

template<class X>
int C1<basic<X> >::f(){
        return 2;

Quote:
}

VC5 shows up the error message

error C2995: 'f' : template function has already been defined

is something wrong in my declaration (to be honest, this would
surprise me, I think I have seen something similar in Stroustrup's)
i.e, it's not legal C++ , or is it an error in Visual C++?

Greetings,
Ralf



Thu, 28 Feb 2002 03:00:00 GMT  
 Template class as template parameter


Quote:
> everything is fine, but defining  instead

> template<class X> basic{
> //...
> }

> template<class X>
> int C1<basic<X> >::f(){
> return 2;
> }

> VC5 shows up the error message

> error C2995: 'f' : template function has already been defined

This is called partial specialization (your are specializing
a function over a restricted domain of types... basic<X>).

First off, you can't partially specialize member functions
(or any function AFAIK, only classes).

Second, VC doesn't support partial specialization of any kind
yet... not even VC6.

marco



Thu, 28 Feb 2002 03:00:00 GMT  
 Template class as template parameter
Hello,

On Sun, 12 Sep 1999 10:46:03 -0700, "Marco Dalla Gasperina"

Quote:

>This is called partial specialization (your are specializing
>a function over a restricted domain of types... basic<X>).

Thanks for the information!

Quote:

>First off, you can't partially specialize member functions
>(or any function AFAIK, only classes).

Did I understand you correctly: it's not possible to do so in C++ in
general?  I mean, here's exactly what I've seen in "The C++
Programming Language" by Stroustrup, 3rd edtion; in chapter 17.6.2.3
the code goes as follows:

I.
template<class T> struct Hash : unary function <T, size_t>{
        size_t operator() (const T& key) const;

Quote:
};

II.
template<class T>size_t Hash<T>::operator() (const T& key) const
{
//...

Quote:
}

III.
template<class C>
size_t Hash<basic_string<C> >::operator() (const basic_string<C>& key)
const
{
//...

Quote:
}

In Chapter 13.5. (on specialisation) he defines

template<class T> class Vector

and later on specializes the swap function

template<class T> void swap(T& x, T& y)

for vectors:

template<class T> void swap (Vector<T> a, Vector<T>& a)

Isn't this a partial specialisation for swap, or didn't I get the
meaning of it correctly?

Indeed in the 2nd edition of C++ P.L. specialization is touched only
slightly; I just browsed the chapter on templates, and it seems,
partial specialiszations are not mentioned at all. Seems to be a new
feature, isn't it?

Quote:
>Second, VC doesn't support partial specialization of any kind
>yet... not even VC6.

Surprise, surprise.

Greetings,
Ralf



Fri, 01 Mar 2002 03:00:00 GMT  
 Template class as template parameter


Quote:
> Hello,

> On Sun, 12 Sep 1999 10:46:03 -0700, "Marco Dalla Gasperina"

> >This is called partial specialization (your are specializing
> >a function over a restricted domain of types... basic<X>).

> Thanks for the information!

You too... I learn something new everyday.

Quote:
> Did I understand you correctly: it's not possible to do so in C++ in
> general?  I mean, here's exactly what I've seen in "The C++
> Programming Language" by Stroustrup, 3rd edtion; in chapter 17.6.2.3
> the code goes as follows:

Well, you understood me, but I was wrong.

There is wording in the standard to allow function template
partial specializations, as well as an example which uses
it. (There isn't a section on it as there is for classes,
however.)

As far as member functions go, I can't find any wording at all
that implies them or even an example.  But, I bow to Mr.
Stroustrup as his spell casting ability is far more powerful
than mine.

Sorry for the error.

marco



Fri, 01 Mar 2002 03:00:00 GMT  
 Template class as template parameter
Hello,

On Mon, 13 Sep 1999 08:59:15 -0700, "Marco Dalla Gasperina"

Quote:

>Sorry for the error.

don't mind. Some points became clearer to me while solving this
puzzle.

Greetings,
Ralf



Fri, 01 Mar 2002 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Template class as template parameter

2. VC7 does NOT support template-template parameters involving non-type parameters

3. template functions in template classes declared outside the class definition

4. Syntax for declaring a template class as a friend of a non-template class

5. friend template class within template class

6. Class Template problem: The linker can't find my class template

7. VC 7.1 final beta: bug with template template parameters

8. template template parameter with default value (VS.NET 2003)

9. Possible bug with template template parameters in VC++ 7.0

10. Bug in inheritance from template template parameters in VC7

11. Templates as a templates-parameters

12. template template parameters

 

 
Powered by phpBB® Forum Software