 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Burnce Guest
|
Posted: Fri Jun 17, 2005 10:25 am Post subject: Template question (specialization of class within a class) |
|
|
Hi,
I have the following problem. I am writing a template class within a
template class. If I want to make a partial or complete spacialization of
the class within the class, I keep getting errors. Is my syntax wrong, or
is it impossible to do?
Consider this example:
///////////// Beginning of example /////////////
#include <iostream> using
namespace std;
template <typename Ta>
class A{
public:
void test();
template <typename Tb>
class B{
public:
void test();
};
protected:
B<Ta> b;
};
int main(){
A<int> aInt;
aInt.test();
A<char> aChar;
aChar.test();
return EXIT_SUCCESS;
}
template <typename Ta>
void A<Ta>::test(){
b.test();
}
template <typename Ta>
template <typename Tb>
void A<Ta>::B<Tb>::test(){
cout <<"This is a test"<
}
template
template <>
void A<Ta>::B<char>::test(){
cout <<"This is a char test"<
}
//////////////// End of example ////////////////
In G++ I get the following output:
example.cc:39: error: invalid explicit specialization before '>' token
example.cc:39: error: enclosing class templates are not explicitly specialized
example.cc:40: error: no member function `test' declared in `A<Ta>::B<char>'
example.cc:40: error: invalid function declaration
If I remove the "template<>" line, I get:
example.cc:40: error: too few template parameter lists in declaration of
..... `void A<Ta>::B<char>::test()'example.cc:40: error: invalid use of
undefined type `class A<Ta>::B<char>' example.cc:10: error: declaration of
`class A<Ta>::B<char>' example.cc:40: error: template definition of
non-template `void A<Ta>::B<char>::test()'
Thanks for any help with this :)
Cheers,
Burnce
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Graeme Prentice Guest
|
Posted: Tue Jun 21, 2005 9:56 am Post subject: Re: Template question (specialization of class within a clas |
|
|
On 17 Jun 2005 06:25:05 -0400, Burnce wrote:
| Quote: | Hi,
I have the following problem. I am writing a template class within a
template class. If I want to make a partial or complete spacialization of
the class within the class, I keep getting errors. Is my syntax wrong, or
is it impossible to do?
|
Your syntax is illegal. You can't explicitly specialize a member
template without explicitly specializing all enclosing templates.
| Quote: |
template <typename Ta
template
void A
cout <<"This is a char test"<
}
|
The above is illegal because the inner template is specialized but the
outer one is not.
The following is legal
template <>
template <>
void A<int>::B<char>::test(){
cout <<"This is a char test"<
}
You have to work slightly harder to achieve what you want. Here's one
way.
#include
using namespace std;
template <typename T1>
struct detect_char
{
};
template <>
struct detect_char<char>
{
};
template <typename Ta>
class A{
public:
void test();
template <typename Tb>
class B{
public:
void test();
template <typename T2>
void test_helper(T2)
{ cout << "nnot char test"; }
void test_helper(detect_char
{ cout << "nchar test success"; }
};
protected:
B
};
int main(){
A<int> aInt;
aInt.test();
A<char> aChar;
aChar.test();
return EXIT_SUCCESS;
}
template <typename Ta>
void A<Ta>::test(){
b.test();
}
template <typename Ta>
template <typename Tb>
void A<Ta>::B<Tb>::test(){
test_helper(detect_char<Tb>());
}
Graeme
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ferdi Smit Guest
|
Posted: Tue Jun 21, 2005 10:01 am Post subject: Re: Template question (specialization of class within a clas |
|
|
"Burnce" <Burnce (AT) gmx (DOT) net> wrote
| Quote: | Hi,
I have the following problem. I am writing a template class within a
template class. If I want to make a partial or complete spacialization of
the class within the class, I keep getting errors. Is my syntax wrong, or
is it impossible to do?
Consider this example:
|
I think you need to refer to this part of the standard:
14.7.3
17In an explicit specialization declaration for a member of a class tem-
plate or a member template that appears in namespace scope, the member
template and some of its enclosing class templates may remain unspe-
cialized, except that the declaration shall not explicitly specialize
a class member template if its enclosing class templates are not
explicitly specialized as well. In such explicit specialization dec-
laration, the keyword template followed by a template-parameter-list
shall be provided instead of the template<> preceding the explicit
specialization declaration of the member. The types of the template-
parameters in the template-parameter-list shall be the same as those
specified in the primary template definition. [Example:
template<class T1> class A {
template<class T2> class B {
template<class T3> void mf1(T3);
void mf2();
};
};
template<> template<class X> class A<int>::B { };
template<> template<> template<class T> void A<int>::B<double>::mf1(T t)
{ };
template<class Y> template<> void A<Y>::B<double>::mf2() { }; //
ill-formed
// B<double> specialized but its enclosing class template A is not
--end example]
Your question conforms directly to the last example.
--
Regards,
Ferdi Smit
smit xs4all nl
[ 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
|
|