Topic: Template specialization inside a template class


Author: "Andrei Alexandrescu" <alexandrescua@micromodeling.com>
Date: 1999/03/02
Raw View
Biju Thomas wrote in message <36D98661.37F556C1@ibm.net>...
>Also, I tried it on egcs and it works. It looks
>like an omission from the grammar.

I also tried it with egcs 1.1.1 (mingw32) and it doesn't work. What
version are you using?

Andrei
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Biju Thomas <bijuthom@ibm.net>
Date: 1999/03/03
Raw View
Andrei Alexandrescu wrote:
>
> Biju Thomas wrote in message <36D98661.37F556C1@ibm.net>...
> >Also, I tried it on egcs and it works. It looks
> >like an omission from the grammar.
>
> I also tried it with egcs 1.1.1 (mingw32) and it doesn't work. What
> version are you using?

This was the example I tried:

  template <class T > class A
  {
  public:
    template <class V> class B { };
    template <> class B<T> { };
  };

I was just trying to see whether this syntax (explicit specialization
within a class definition) is accepted by the compiler. (On egcs-2.91.57
19980901).

But, beyond the parsing level, when I try to use the class, it gives
errors, access violations etc. I gave up.

--
Best regards,
Biju Thomas
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: "Andrei Alexandrescu" <alexandrescua@micromodeling.com>
Date: 1999/02/26
Raw View
Is the following legal?

template <class T, class U = int>
class A
{
    template <class V> class B
    {
        ...
    };
    template <> class B<T>
    {
        ...
    };
    ...
};

Andrei



[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Alexandre Oliva <oliva@dcc.unicamp.br>
Date: 1999/02/27
Raw View
On Feb 26, 1999, "Andrei Alexandrescu" <alexandrescua@micromodeling.com> wrote:

> Is the following legal?

> template <class T, class U = int> class A {
>     template <class V> class B { ...  };
>     template <> class B<T> { ...  };

Nope, the C++ grammar doesn't accept explicit-specializations as
member-declarations.

--
Alexandre Oliva http://www.dcc.unicamp.br/~oliva aoliva@{acm.org,computer.org}
oliva@{dcc.unicamp.br,gnu.org,egcs.cygnus.com,samba.org}
Instituto de Computa      o, Universidade Estadual de Campinas, SP, Brasil


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Gabriel Dos_Reis <gdosreis@sophia.inria.fr>
Date: 1999/02/27
Raw View
Alexandre Oliva <oliva@dcc.unicamp.br> writes:

=B7-----------------
| On Feb 26, 1999, "Andrei Alexandrescu" <alexandrescua@micromodeling.com=
> wrote:
|=20
| > Is the following legal?
|=20
| > template <class T, class U =3D int> class A {
| >     template <class V> class B { ...  };
| >     template <> class B<T> { ...  };
|=20
| Nope, the C++ grammar doesn't accept explicit-specializations as
| member-declarations.
=B7-----------------

Is that an oversight or is there any reason to decide not to allow
explicit-specializations as member-declarations?

--=20
Gabriel Dos Reis, dosreis@cmla.ens-cachan.fr
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: sbnaran@bardeen.ceg.uiuc.edu (Siemel Naran)
Date: 1999/02/28
Raw View
On 27 Feb 1999 15:57:07 GMT, Alexandre Oliva <oliva@dcc.unicamp.br> wrote:
>On Feb 26, 1999, "Andrei Alexandrescu" <alexandrescua@micromodeling.com> wrote:

>> Is the following legal?
>
>> template <class T, class U = int> class A {
>>     template <class V> class B { ...  };
>>     template <> class B<T> { ...  };

>Nope, the C++ grammar doesn't accept explicit-specializations as
>member-declarations.

I think Andrei's code is legal.

Specializations of template entities of a template class outside the
class definition are not allowed because you may be specializing
something which does not exist.  In fact, the standard explicitly
prohibits this.
   template <class T> class A { template <class V> class B { V v; }; }; // LINE1
   template <class T> template <> class A<T>::B<int> { }; // LINE2
Now consider this specialization of A<void>
   template <> class A<void> { }; // no nested class B, so LINE2 makes no sense

But specializations within the class definition are entirely reasonable.
There is an example of this in the standard too.  Consider:
   template <class T> class A {
      template <class V> class B { V v; };
      template <> class B<int> { };
   };

--
----------------------------------
Siemel B. Naran (sbnaran@uiuc.edu)
----------------------------------
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Biju Thomas <bijuthom@ibm.net>
Date: 1999/03/01
Raw View
Siemel Naran wrote:
>
> On 27 Feb 1999 15:57:07 GMT, Alexandre Oliva <oliva@dcc.unicamp.br> wrote:
>
> >Nope, the C++ grammar doesn't accept explicit-specializations as
> >member-declarations.
>
> I think Andrei's code is legal.
>

I cannot think of a rule in the grammar which allows this. Did you find
a way?

But, the standard talks about explicit specialization of member class
templates in 14.7.3. Also, I tried it on egcs and it works. It looks
like an omission from the grammar.

--
Best regards,
Biju Thomas
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]