 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
matt Guest
|
Posted: Sun Jan 23, 2005 3:02 am Post subject: template template partial specialisation advice needed |
|
|
Hi All,
| Quote: | From spending an hour reading previous posts to this group, I have
found these types of questions continually arise. Here is another that |
I have been wrestling with that appears not to have been specifically
discussed. I have been having problems with the following simpler
scenario (ie in a short form):
template <typename A>
class B {
....
};
template <template
class E {
typedef typename D::some_type some_type;
....
some_type doIt();
....
};
--- compiles fine up to here ---
template <typename D>
E<B, D>::some_type
E<B, D>::doIt() { <== errors appear for this line
....
}
gcc version 3.4.2 20041017 (Red Hat 3.4.2-6.fc3) complains:
expected init declarator before 'E'
expected ; before 'E'
In other words, I would like to specialise E::doIt() for the case where
B is the supplied model of C. Somehow I thought this would be as easy
as a template partial specialization... :-)
Thanks, Matt
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Efrat Regev Guest
|
Posted: Sun Jan 23, 2005 10:16 am Post subject: Re: template template partial specialisation advice needed |
|
|
"matt" <dmatt001 (AT) yahoo (DOT) com> wrote
| Quote: | Hi All,
In other words, I would like to specialise E::doIt() for the case where
B is the supplied model of C. Somehow I thought this would be as easy
as a template partial specialization...
|
From looking at your code, it seems indeed that your problem does not
lie with template templates. Here is a simplification of
your code (I've eliminated template templates):
// For simplicity, B is now not a template
class B{};
// For simplicity, C is not, itself, a template
template<typename C, typename D>
class E
{
typedef typename D::some_type some_type;
some_type doIt();
};
/*****************/Problems!
**************************
// Following is problematic (won't compile)
template<typename D>
E<B, D>::some_type // especially, this line
E<B, D>::doIt()
{
}
/*****************/Problems!
**************************
Now your modified code contains no template templates, but still won't
compile. As far as I undersand, the code I surrounded by asterikses has two
problems:
1. The compiler sees a method of a specialization of E which is never
declared.
2. It has no idea what is E<B, D>::some_type
Here's how to fix this (your original version):
template<typename A>
class B{};
template<template
class E
{
typedef typename D::some_type some_type;
some_type doIt();
};
// Specialize the entire class
template<typename D>
class E<B, D>
{
typedef typename D::some_type some_type;
some_type doIt();
};
template<typename D>
// notice change in next line
typename E<B, D>::some_type
E<B, D>::doIt()
{
}
____________________________________________________
(Note that while the code above compiles, it is problematic. E.g., the
line "typedef typename D::some_type some_type;" appears twice. This can be
solved by various means, for example, by inheriting both the primary E and
specialized E from the same base class).
Possibly, you might want to read "C++ Templates: the Complete Guide" by
Vandevoorde and Jossutis.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Carl Barron Guest
|
Posted: Sun Jan 23, 2005 10:18 am Post subject: Re: template template partial specialisation advice needed |
|
|
In article <1106438996.985790.273250 (AT) c13g2000cwb (DOT) googlegroups.com>,
matt <dmatt001 (AT) yahoo (DOT) com> wrote:
| Quote: | Hi All,
From spending an hour reading previous posts to this group, I have
found these types of questions continually arise. Here is another that
I have been wrestling with that appears not to have been specifically
discussed. I have been having problems with the following simpler
scenario (ie in a short form):
template <typename A
class B {
...
};
template
class E {
typedef typename D::some_type some_type;
...
some_type doIt();
...
};
--- compiles fine up to here ---
template <typename D
E
E<B, D>::doIt() { <== errors appear for this line
...
}
|
templates can be partially specialized in total. If the classes are
large
something like: Not tested exposition only.
template
struct EBase
{
void do_it() ; // implments generic do_it.
};
template <typename D>
struct EBase<B,D>
{
voiid do_it(); // impliemts specialized do_it.
};
template <template
class E :public EBase<C,D>
{
friend class EBase<C,D>;
public:
// etc. execpt use the base class's do_it()
};
is an alternative to writing the duplicate code...
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|