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

how to partially specialize a template class nested inside anothertemplate class?

For example, like in the following, the part commented out was
intended as partial spectialzation, but it would even compile. Is it
even legal to partially specialize a nested template class inside
another template class?

template < typename T >
struct A
{
template < typename U >
struct B
{
static const int v = 1;
};

/* template < // this doesn't compile
struct B < int >
{
static const int v = 3;
};
*/
};
Jun 27 '08 #1
5 3710
On Jun 22, 12:15*am, huil...@gmail.com wrote:
For example, like in the following, the part commented out was
intended as partial spectialzation, but it would even compile. *Is it
even legal to partially specialize a nested template class inside
another template class?

template < typename T >
struct A
{
* * * * template < typename U >
* * * * struct B
* * * * {
* * * * * * * * static const int v = 1;
* * * * };

/* * * *template < // this doesn't compile
* * * * struct B < int >
* * * * {
* * * * * * * * static const int v = 3;
* * * * };
*/

};

Sorry, when I said the could "would even compile", I meant "wouldn't
even compile". Sorry about this typo.
Jun 27 '08 #2
On Jun 21, 9:15*pm, huil...@gmail.com wrote:
For example, like in the following, the part commented out was
intended as partial spectialzation, but it would even compile. *Is it
even legal to partially specialize a nested template class inside
another template class?

template < typename T >
struct A
{
* * * * template < typename U >
* * * * struct B
* * * * {
* * * * * * * * static const int v = 1;
* * * * };

/* * * *template < // this doesn't compile
* * * * struct B < int >
* * * * {
* * * * * * * * static const int v = 3;
* * * * };
*/

};
Yes, it is possible to partially specialize a nested class template in
C++ (without having to specialize the enclosing class template). It is
not legal however to explicitly specialize a nested class template (if
the enclosing class template is not explicitly specialized as well).
In fact, the example program demonstrates this last point by defining
a B<intexplicit specialization - without explicitly specializing A
as well.

In other words, the problem with the above program is that B<intis
not a partial specialization - but a complete specialization of B.

Greg

Jun 27 '08 #3
On Jun 22, 6:33*am, Greg Herlihy <gre...@mac.comwrote:
On Jun 21, 9:15*pm, huil...@gmail.com wrote:
For example, like in the following, the part commented out was
intended as partial spectialzation, but it would even compile. *Is it
even legal to partially specialize a nested template class inside
another template class?
template < typename T >
struct A
{
* * * * template < typename U >
* * * * struct B
* * * * {
* * * * * * * * static const int v = 1;
* * * * };
/* * * *template < // this doesn't compile
* * * * struct B < int >
* * * * {
* * * * * * * * static const int v = 3;
* * * * };
*/
};

Yes, it is possible to partially specialize a nested class template in
C++ (without having to specialize the enclosing class template). It is
not legal however to explicitly specialize a nested class template (if
the enclosing class template is not explicitly specialized as well).
In fact, the example program demonstrates this last point by defining
a B<intexplicit specialization - without explicitly specializing A
as well.

In other words, the problem with the above program is that B<intis
not a partial specialization - but a complete specialization of B.

Greg
Thanks! But how do I understand the example in the following. Here
B<C<V looks like an explicit specialization just like B<int>, since
C is just another template independent of A or A::B. But now it
compiles on apple-g++-4.0, and gives the correct (expected) result (no
exception was thrown).

-------------------------------------------------------------

template < typename T >
struct C {};

template < typename T >
struct A
{
template < typename U >
struct B
{
static const int v = 1;
};

template < typename V >
struct B< C<V
{
static const int v = 3;
};
};

int main()
{
if ( A<int>::B<double>::v != 1 )
throw;

if ( A<int>::B< C<double::v != 3 )
throw;

return 0;
};

Jun 27 '08 #4
On Jun 22, 9:29*am, huil...@gmail.com wrote:
On Jun 22, 6:33*am, Greg Herlihy <gre...@mac.comwrote:
On Jun 21, 9:15*pm, huil...@gmail.com wrote:
For example, like in the following, the part commented out was
intended as partial spectialzation, but it would even compile. *Is it
even legal to partially specialize a nested template class inside
another template class?
template < typename T >
struct A
{
* * * * template < typename U >
* * * * struct B
* * * * {
* * * * * * * * static const int v = 1;
* * * * };
/* * * *template < // this doesn't compile
* * * * struct B < int >
* * * * {
* * * * * * * * static const int v = 3;
* * * * };
*/
};
Yes, it is possible to partially specialize a nested class template in
C++ (without having to specialize the enclosing class template). It is
not legal however to explicitly specialize a nested class template (if
the enclosing class template is not explicitly specialized as well).
In fact, the example program demonstrates this last point by defining
a B<intexplicit specialization - without explicitly specializing A
as well.
In other words, the problem with the above program is that B<intis
not a partial specialization - but a complete specialization of B.
Greg

Thanks! But how do I understand the example in the following. Here
B<C<V looks like an explicit specialization just like B<int>, since
C is just another template independent of A or A::B. *But now it
compiles on apple-g++-4.0, and gives the correct (expected) result (no
exception was thrown).

-------------------------------------------------------------

template < typename T >
struct C {};

template < typename T >
struct A
{
* * * * template < typename U >
* * * * struct B
* * * * {
* * * * * * * * static const int v = 1;
* * * * };

* * * * template < typename V >
* * * * struct B< C<V
* * * * {
* * * * * * * * static const int v = 3;
* * * * };

};

int main()
{
* * * * if ( A<int>::B<double>::v != 1 )
* * * * * * * * throw;

* * * * if ( A<int>::B< C<double::v != 3 )
* * * * * * * * throw;

* * * * return 0;

};

Never mind.... B<C<V is not an explicit specialization. What was I
thinking!!!
Jun 27 '08 #5
On Jun 22, 6:33*am, Greg Herlihy <gre...@mac.comwrote:
On Jun 21, 9:15*pm, huil...@gmail.com wrote:
For example, like in the following, the part commented out was
intended as partial spectialzation, but it would even compile. *Is it
even legal to partially specialize a nested template class inside
another template class?
template < typename T >
struct A
{
* * * * template < typename U >
* * * * struct B
* * * * {
* * * * * * * * static const int v = 1;
* * * * };
/* * * *template < // this doesn't compile
* * * * struct B < int >
* * * * {
* * * * * * * * static const int v = 3;
* * * * };
*/
};

Yes, it is possible to partially specialize a nested class template in
C++ (without having to specialize the enclosing class template). It is
not legal however to explicitly specialize a nested class template (if
the enclosing class template is not explicitly specialized as well).
In fact, the example program demonstrates this last point by defining
a B<intexplicit specialization - without explicitly specializing A
as well.

In other words, the problem with the above program is that B<intis
not a partial specialization - but a complete specialization of B.

Greg
Hi, I found a way (among other possibilities) to workaround impossible
explicit specialization of nested template classes.
It works!
---------------------------------------------------------------------
template < typename struct A; // forward declaration of A
template < typename T, typename t >
struct C
{
typedef A<t> AT;
static const int v = 1;
};

template < typename t >
struct C<int,t>
{
typedef A<t> AT;
static const int v = 3;
};
template < typename T >
struct A
{
template < typename U >
struct B : C<U,T>
{
typename C<U,T>::AT const& _a;
B(typename C<U,T>::AT const& a):_a(a){}
};

B<intb;
A():b(*this){}
};
int main()
{
A<inta;
A<int>::B<doubleb1(a);

if ( b1.v != 1 )
throw;

if ( a.b.v != 3 )
throw;

return 0;
};
Jun 27 '08 #6

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

Similar topics

4
by: Old Wolf | last post by:
I have a template class like this: template<typename T, int N> struct Foo { ........... }; which I have successfully specialized for some types, eg:
2
by: Dominik Fritz | last post by:
Hi, I have a template class which contains a nested class. The .cpp file is included at the end of the header file. If I write the implementation of a member function of my nested class in this...
1
by: mrstephengross | last post by:
Ok, I've got a class with two template parameters (A and B), and a member function foo(). I want to specialize foo for a particular class A. Is this possible? The following code shows an example: ...
3
by: PengYu.UT | last post by:
I have the following two program. The first one compiles well, but the second one doesn't. The only difference between them is one more template parameter is added for the second program. Would...
3
by: toton | last post by:
Hi, I want to specialize template member function of a template class . It is creating some syntax problem .... Can anyone say how to do it ? The class is something like this template<typename...
16
by: PengYu.UT | last post by:
Hi, I want to partial specialize the member function doit. But it doesn't work. Could you please help me to figure out what is wrong? Thanks, Peng template <typename T> class A {
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:
7
by: mathieu | last post by:
Hi there, I know this is not possible in c++. So my question, how should I rewrite the following piece of code (without using a dummy class which template parameter could be use for partial...
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
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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.