Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Template class partial specialisation problem

Reply
Thread Tools

Template class partial specialisation problem

 
 
Lionel B
Guest
Posts: n/a
 
      11-03-2004
Greetings.

The following code compiles ok and does what I'd expect it to do:

[apologies for the lack of indentation... Google Groups 2 Beta bug
reported]

---------- START CODE ----------

// test.cpp

#include <iostream>

template <class T> struct A
{
A();
};

A<int>::A()
{
std::cout << "Template parameter is \"int\"" << '\n';
}

A<long>::A()
{
std::cout << "Template parameter is \"long\"" << '\n';
}

int main()
{
A<int> a1;
A<long> a2;

return 0;
}

---------- END CODE ----------

The program outputs:

Template parameter is "int"
Template parameter is "long"

So far so good... now I would like to do the same, but there is a
second template parameter and I only want to specialise on the first:

---------- START CODE ----------

// test.cpp

#include <iostream>

template <class T, class R> struct A
{
A();
};

template <class R> A<int, R>::A() // line 10
{
std::cout << "First template parameter is \"int\"" << '\n';
}

template <class R> A<long, R>::A() // line 15
{
std::cout << "First template parameter is \"long\"" << '\n';
}

int main()
{
A<int, int> a1;
A<long, int> a2;

return 0;
}

---------- END CODE ----------

My compiler (gcc 3.3.3 cygwin special) throws this out with:

g++ test.cpp -o test.exe
test.cpp:10: error: parse error before `)' token
test.cpp:15: error: parse error before `)' token

I can't figure out quite what the syntax should be to specialise on the
first template parameter only. It would seem that A<int, R>::A() is
still a template function, but my compiler doesn't like the "obvious"
definition:
template <class R> A<int, R>::A()

Any help appreciated,

--
Lionel B

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      11-03-2004
Lionel B wrote:
> Greetings.
>
> The following code compiles ok and does what I'd expect it to do:
>
> [apologies for the lack of indentation... Google Groups 2 Beta bug
> reported]
>
> ---------- START CODE ----------
>
> // test.cpp
>
> #include <iostream>
>
> template <class T> struct A
> {
> A();
> };
>
> A<int>::A()
> {
> std::cout << "Template parameter is \"int\"" << '\n';
> }
>
> A<long>::A()
> {
> std::cout << "Template parameter is \"long\"" << '\n';
> }
>
> int main()
> {
> A<int> a1;
> A<long> a2;
>
> return 0;
> }
>
> ---------- END CODE ----------
>
> The program outputs:
>
> Template parameter is "int"
> Template parameter is "long"
>
> So far so good... now I would like to do the same, but there is a
> second template parameter and I only want to specialise on the first:
>
> ---------- START CODE ----------
>
> // test.cpp
>
> #include <iostream>
>
> template <class T, class R> struct A
> {
> A();
> };
>
> template <class R> A<int, R>::A() // line 10
> {
> std::cout << "First template parameter is \"int\"" << '\n';
> }
>
> template <class R> A<long, R>::A() // line 15
> {
> std::cout << "First template parameter is \"long\"" << '\n';
> }
>
> int main()
> {
> A<int, int> a1;
> A<long, int> a2;
>
> return 0;
> }
>
> ---------- END CODE ----------
>
> My compiler (gcc 3.3.3 cygwin special) throws this out with:
>
> g++ test.cpp -o test.exe
> test.cpp:10: error: parse error before `)' token
> test.cpp:15: error: parse error before `)' token
>
> I can't figure out quite what the syntax should be to specialise on the
> first template parameter only. It would seem that A<int, R>::A() is
> still a template function, but my compiler doesn't like the "obvious"
> definition:
> template <class R> A<int, R>::A()


I believe to partially specialise a member, you need to partially
specialise the class first. You need to add these lines before the
member specialisations:

template<class R> struct A<int,R> { A(); };
template<class R> struct A<long,R> { A(); };

Victor
 
Reply With Quote
 
 
 
 
Lionel B
Guest
Posts: n/a
 
      11-03-2004
Victor Bazarov wrote:
> Lionel B wrote:


/snip/

> > I can't figure out quite what the syntax should be to specialise
> > on the first template parameter only. It would seem that
> > A<int, R>::A() is still a template function, but my compiler
> > doesn't like the "obvious" definition:
> > template <class R> A<int, R>::A()

>
> I believe to partially specialise a member, you need to partially
> specialise the class first. You need to add these lines before the
> member specialisations:
>
> template<class R> struct A<int,R> { A(); };
> template<class R> struct A<long,R> { A(); };


Cheers, Victor. But that leaves me with a problem; it seems then that I
have to partially specialise *every member function* in the class.
e.g.:

---------- START CODE ----------

// test.cpp

#include <iostream>

template <class T, class R> struct A
{
A();
void foo() {}
};

template<class R> struct A<int,R> {A();};
template<class R> A<int, R>::A()
{
std::cout << "First template parameter is \"int\"" << '\n';
}

template<class R> struct A<long,R> {A();};
template<class R> A<long, R>::A()
{
std::cout << "First template parameter is \"long\"" << '\n';
}

int main()
{
A<int, int> a1;
A<long, int> a2;

a1.foo();
a2.foo();

return 0;
}

---------- END CODE ----------

g++ test.cpp -o test.exe
test.cpp: In function `int main()':
test.cpp:28: error: `foo' undeclared (first use this function)
test.cpp:28: error: (Each undeclared identifier is reported only once
for each
function it appears in.)

Problem is my class has *many* member functions, but only a handful of
them need to be partially specialised. Surely I shouldn't have to
re-write identical code for every member function that does not need to
be partially specialised?

--
Lionel B



Now this is really not on for my class, as there is no way I

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      11-03-2004
Lionel B wrote:
> Victor Bazarov wrote:
>
>>Lionel B wrote:

>
>
> /snip/
>
>
>>>I can't figure out quite what the syntax should be to specialise
>>>on the first template parameter only. It would seem that
>>>A<int, R>::A() is still a template function, but my compiler
>>>doesn't like the "obvious" definition:
>>>template <class R> A<int, R>::A()

>>
>>I believe to partially specialise a member, you need to partially
>>specialise the class first. You need to add these lines before the
>>member specialisations:
>>
>>template<class R> struct A<int,R> { A(); };
>>template<class R> struct A<long,R> { A(); };

>
>
> Cheers, Victor. But that leaves me with a problem; it seems then that I
> have to partially specialise *every member function* in the class.


Why?

> e.g.:
>
> ---------- START CODE ----------
>
> // test.cpp
>
> #include <iostream>
>
> template <class T, class R> struct A
> {
> A();
> void foo() {}
> };
>
> template<class R> struct A<int,R> {A();};
> template<class R> A<int, R>::A()
> {
> std::cout << "First template parameter is \"int\"" << '\n';
> }
>
> template<class R> struct A<long,R> {A();};
> template<class R> A<long, R>::A()
> {
> std::cout << "First template parameter is \"long\"" << '\n';
> }
>
> int main()
> {
> A<int, int> a1;
> A<long, int> a2;
>
> a1.foo();
> a2.foo();
>
> return 0;
> }
>
> ---------- END CODE ----------
>
> g++ test.cpp -o test.exe
> test.cpp: In function `int main()':
> test.cpp:28: error: `foo' undeclared (first use this function)
> test.cpp:28: error: (Each undeclared identifier is reported only once
> for each
> function it appears in.)
>
> Problem is my class has *many* member functions, but only a handful of
> them need to be partially specialised. Surely I shouldn't have to
> re-write identical code for every member function that does not need to
> be partially specialised?


By partial specialisation of a template class you create another template.
You're responsible for its contents. If you say that it's not going to
have the 'foo' member, it's not going to have the 'foo' member. But if
you try to use the 'foo' member, you get an error. You're basically
contradicting yourself here. OOH you say, here is a special variation of
my A template, see? It doesn't have a foo function. Then you say, no I
do want it to have a foo function. So, which is it? It's a rhetorical
question...

One thing you should remember, there is no partial specialisation of
function templates. None. Not allowed. That basically means no partial
specialisations of member functions, either, by themselves. What you are
allowed to create is a full specialisation of a member of another class
template, who just happens to be a partial specialisation of your original
class template.

Such is life.

>
> --
> Lionel B
>
>
>
> Now this is really not on for my class, as there is no way I


Huh? Sorry, I can't say I understand what you're trying to say here.
 
Reply With Quote
 
Lionel B
Guest
Posts: n/a
 
      11-03-2004
Victor Bazarov wrote:
> Lionel B wrote:
> > Victor Bazarov wrote:
> >
> >>Lionel B wrote:

> >
> > /snip/
> >
> >>>I can't figure out quite what the syntax should
> >>>be to specialise on the first template parameter
> >>>only. It would seem that A<int, R>::A() is still
> >>>a template function, but my compiler doesn't like
> >>>the "obvious" definition:
> >>>template <class R> A<int, R>::A()
> >>
> >>I believe to partially specialise a member, you need
> >>to partially specialise the class first. You need to
> >>add these lines before the member specialisations:
> >>
> >>template<class R> struct A<int,R> { A(); };
> >>template<class R> struct A<long,R> { A(); };

> >
> > Cheers, Victor. But that leaves me with a problem; it
> > seems then that I have to partially specialise *every
> > member function* in the class.

>
> Why?


Read on.

/snip/

> > Problem is my class has *many* member functions, but only
> > a handful of them need to be partially specialised. Surely
> > I shouldn't have to re-write identical code for every member
> > function that does not need to be partially specialised?

>
> By partial specialisation of a template class you create another
> template. You're responsible for its contents. If you say that
> it's not going to have the 'foo' member, it's not going to have
> the 'foo' member. But if you try to use the 'foo' member, you
> get an error.


What I would really like is that if a member function - foo, say - is
not explicitly (partially) specialised, then the definition of the
un-specialised foo member from the un-specialised template class is
picked up. This works perfectly well with full specialisation:

---------- BEGIN CODE ----------

#include <iostream>

template <class T> struct A
{
A();
void foo()
{
std::cout << "This is foo" << '\n';
}
};

A<int>::A()
{
std::cout << "Template parameter is \"int\"" << '\n';
}

A<long>::A()
{
std::cout << "Template parameter is \"long\"" << '\n';
}

int main()
{
A<int> a1;
A<long> a2;

a1.foo();
a2.foo();

return 0;
}

---------- END CODE ----------

works perfectly well. Output:

Template parameter is "int"
Template parameter is "long"
This is foo
This is foo

Here foo is simply picked up from its definition in the the
un-specialised template class. Now I honestly don't see why this
couldn't be the case for partial specialisation (evidently it isn't).

To my mind this renders partial specialisation less than useful. If I'm
going to have to write different definitions for every function of a
partial specialisation of a template class, I may as well be done with
it and write separate template classes for each specialisation!

/snip/

> One thing you should remember, there is no partial specialisation
> of function templates. None. Not allowed. That basically means
> no partial specialisations of member functions, either, by
> themselves.


Yup. That's precisely what I want... I suppose there must be a good
reason for this, but I can't see it. As I say, it seems to work ok for
full specialisation.

> What you are
> allowed to create is a full specialisation of a member of another
> class template, who just happens to be a partial specialisation of
> your original class template.


Not sure I follow you here.

> > Now this is really not on for my class, as there is no way I

>
> Huh? Sorry, I can't say I understand what you're trying to say here.

Me neither. Editing glitch.

--
Lionel B

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      11-03-2004
Lionel B wrote:
> [...]
> To my mind this renders partial specialisation less than useful. If I'm
> going to have to write different definitions for every function of a
> partial specialisation of a template class, I may as well be done with
> it and write separate template classes for each specialisation!


But that is what you essentially do when you partially specialise a class
template. You _essentially_ create another template. Sort of.

>>One thing you should remember, there is no partial specialisation
>>of function templates. None. Not allowed. That basically means
>>no partial specialisations of member functions, either, by
>>themselves.

>
>
> Yup. That's precisely what I want... I suppose there must be a good
> reason for this, but I can't see it. As I say, it seems to work ok for
> full specialisation.


See "C++ Templates" by Vandevoorde and Josuttis, section 13.7. I am too
lazy to re-type it all here.

>>What you are
>>allowed to create is a full specialisation of a member of another
>>class template, who just happens to be a partial specialisation of
>>your original class template.

>
>
> Not sure I follow you here.


I am not sure I follow myself. Never mind. Get the Vandevoorde/Josuttis
book and many things are going to be clearer.

V
 
Reply With Quote
 
Andre Dajd
Guest
Posts: n/a
 
      11-04-2004
"Lionel B" <(E-Mail Removed)> wrote in message news:<(E-Mail Removed) roups.com>...
> Greetings.


<snip>

You may solve your issues by combining templates with inheritance.
You write one (or many) base template class(es) and then subclass from
them with reduced set of template parameters. This will pull all
virtuals form the base which will be automatically specialized, but
which you will still be able to overwrite.

This will not always work, precisely because ALL base class function
will be specialized in this approach (following standard instantiation
rules), which may result in some illegal code being generated. That's
why one may need to finer slice the superclass into several classes.

Also, this will work only when the context will allow you to use the
inherited methods polymorphically. Eg, it will not work in
constructors.

Back to Earth, your example could mutate into something like this:

/******/
#include <iostream>
#include <string>

// need these (or comparable) functions that implement static
polymorphism
// because constructors can only be statically polymorphic
template<typename _T>
static std::string const&
TypeToString();

template<>
static std::string const&
TypeToString<int>()
{
static std::string const ret = std::string("int");
return ret;
}

template<>
static std::string const&
TypeToString<long>()
{
static std::string const ret = std::string("long");
return ret;
}

template<>
static std::string const&
TypeToString<double>()
{
static std::string const ret = std::string("double");
return ret;
}

// now the main thing stars
template<typename _First, typename _Second>
struct Base
{
typedef _First First;
typedef _Second Second;

virtual ~Base() {}

Base() { printFirstTemplateParameter(); }

void
printFirstTemplateParameter() { std::cout<<"First parameter type:
"<<TypeToString<First>()<<std::endl; }

virtual void
someOtherFunction() { std::cout<<"UNIX: dont know what to
do!"<<std::endl; }
};

template<typename T>
struct BaseSecedToInt
: public Base<int, T>
{
void
someOtherFunction() { std::cout<<"Windoze: dont know what to
do!"<<std::endl; }
};

template<typename T>
struct BaseSecedToLong
: public Base<long, T>
{
void
someOtherFunction() { std::cout<<"OSX: dont know what to
do!"<<std::endl; }
};


int main(int argc, char *argv[])
{
Base<double, double> b;
b.someOtherFunction();

BaseSecedToInt<double> bi;
bi.someOtherFunction();

BaseSecedToLong<double> bl;
bl.someOtherFunction();

return 0;
}
 
Reply With Quote
 
Lionel B
Guest
Posts: n/a
 
      11-04-2004
Andre Dajd wrote:
> "Lionel B" <(E-Mail Removed)> wrote in message

news:<(E-Mail Removed) roups.com>...
> > Greetings.

>
> <snip>
>
> You may solve your issues by combining templates with
> inheritance. You write one (or many) base template class(es)
> and then subclass from them with reduced set of template parameters.
> This will pull allvirtuals form the base which will be automatically
> specialized, but which you will still be able to overwrite.


I had thought of trying something like that (although from what you
point out below it would not neccesarilly have worked, I suspect) and
rejected it on the grounds of: (a) my class is very low-level and
supposed to be super-efficient - could not afford the overhead of
virtual functions - and (b) it would entail horrifically obscure code,
as your example amply demonstrates

> This will not always work, precisely because ALL base class
> function will be specialized in this approach (following standard
> instantiation rules), which may result in some illegal code being
> generated. That's why one may need to finer slice the superclass
> into several classes.


This would probably be a nightmare in my case.

> Also, this will work only when the context will allow you to use
> the inherited methods polymorphically. Eg, it will not work in
> constructors.


[...]

Thanks,

--
Lionel B

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Template class partial specialisation Adrian Hawryluk C++ 2 03-19-2007 07:39 PM
expression template, two arguments, partial specialisation error Martin Gernhard C++ 2 11-16-2005 12:27 AM
Template partial specialisation/inheritance problem Jules C++ 6 08-18-2005 07:37 PM
Specialisation of a template that results to another template Nicolas Weidmann C++ 1 02-17-2004 10:18 AM
Template specialisation of class with base class template ?! Tim Clacy C++ 12 12-03-2003 05:51 PM



Advertisments