473,417 Members | 1,626 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,417 software developers and data experts.

Template class partial specialisation problem

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

Jul 22 '05 #1
7 2104
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
Jul 22 '05 #2
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

Jul 22 '05 #3
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.
Jul 22 '05 #4
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

Jul 22 '05 #5
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
Jul 22 '05 #6
"Lionel B" <go****@lionelb.com> wrote in message news:<10**********************@f14g2000cwb.googleg 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;
}
Jul 22 '05 #7
Andre Dajd wrote:
"Lionel B" <go****@lionelb.com> wrote in message news:<10**********************@f14g2000cwb.googleg 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

Jul 22 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Paul MG | last post by:
Hi Template partial specialization always seems like a fairly straightforward concept - until I try to do it :). I am trying to implement the input sequence type (from Stroustrup section...
2
by: Simon G Best | last post by:
Hello! I have a query regarding explicit specialisation of class templates which are themselves members of class templates. Here's what I want to do: template< class T > struct pink { ...
1
by: Alfonso Morra | last post by:
if I have a class template declared as ff: (BTW is this a partial specialization? - I think it is) template <typename T1, myenum_1 e1=OK, my_enum_2=NONE> class A { public: A(); virtual...
6
by: rincewind | last post by:
Hi, can anybody summarise all options for partial template specialization, for all kind of parameters (type, nontype, template)? I *think* I understand options for partial specialization on...
1
by: Nathan Addy | last post by:
I am trying to partially specialize a template in the following way: I have two template classes defined: template <typename T> class Foo { public: Foo(); }; template <typename T>
8
by: Paul Roberts | last post by:
Hi, I'm hoping somebody here can help me with a simple problem of template syntax. Here's an example: template<typename T, int iclass A { static int a;
1
by: Martin | last post by:
I'm trying to make a partial specialization of a class of mine. Can someone please tell me what's wrong with the following code? GCC gives me the error "invalid use of undefined type "class X<int,...
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:
9
by: stephen.diverdi | last post by:
Can anyone lend a hand on getting this particular template specialization working? I've been trying to compile with g++ 4.1 and VS 2005. ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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,...
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.