Topic: Template with member templates question


Author: "(C) Chichiang Wan" <wanc@hpclear6.cup.hp.com>
Date: 1997/07/09
Raw View
Don Griffin wrote:

> This did not work as expected.  Should this work?  The following seems
> to at least compile, but I don't understand the meaning of it:
>

The following should not compile.

> template <class T>
> class smartptr
> {
>     template <class I>
>     smartptr (I *p)  { do some magic }
>
>     template <>  // I saw this in STL source; what's it mean?
>     smartptr (T *);
> };
>
> Without the "template<>" above smartptr(T*), the compiler does not like
> the duplicate definition, which makes sense.
>
> Any experts out there who understand this stuff?  Has it stabilized in
> the Standard yet?
>
I don't think myself as an expert but I might know what is wrong with
your example.

template <> ... means you want to EXPLICITLY specialize a previously
defined template.
Explicit specialization, declaration or definiton, has to be in
namescape scope.

--
**********************************************************************
Name: Chichiang Wan
email: wanc@cup.hp.com
Phone: (408) 447-5762
**********************************************************************
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: Alexandre Oliva <oliva@dcc.unicamp.br>
Date: 1997/07/11
Raw View
Don Griffin writes:

>     template <T>
>     smartptr (T *);

> This did not work as expected.  Should this work?

Definitely not.  This is not a valid template declaration.

> The following seems to at least compile, but I don't understand the
> meaning of it:

> template <class T>
> class smartptr
> {
>     template <class I>
>     smartptr (I *p)  { do some magic }

>     template <>  // I saw this in STL source; what's it mean?
>     smartptr (T *);
> };

This means that a template specialization follows.  If you did not
provide the template<>, it would not be a specialization, but a
non-template constructor.

> Without the "template<>" above smartptr(T*), the compiler does not like
> the duplicate definition, which makes sense.

It should accept the duplicate definition, and use it instead of the
templatized version.  Since it is a constructor and constructors do
not have names, the templatized version would never be used for I=T in
this case.

> Has it stabilized in the Standard yet?

I think so.

--
Alexandre Oliva
mailto:oliva@dcc.unicamp.br mailto:aoliva@acm.org
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         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: Don Griffin <dgriffin@farallon.com>
Date: 1997/07/07
Raw View
I was experimenting with using member templates in a template, and I
tried to provide a specialized version of a member template:

template <class T>
class smartptr
{
    template <class I>
    smartptr (I *p)  { do some magic }

    template <T>
    smartptr (T *);
};

This did not work as expected.  Should this work?  The following seems
to at least compile, but I don't understand the meaning of it:

template <class T>
class smartptr
{
    template <class I>
    smartptr (I *p)  { do some magic }

    template <>  // I saw this in STL source; what's it mean?
    smartptr (T *);
};

Without the "template<>" above smartptr(T*), the compiler does not like
the duplicate definition, which makes sense.

Any experts out there who understand this stuff?  Has it stabilized in
the Standard yet?

Thanks to any takers!
Don Griffin
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]