Topic: Defining member function templates of class templates out of line


Author: Nicola Musatti <nicola.musatti@gmail.com>
Date: Thu, 24 May 2007 13:39:22 CST
Raw View
Hallo,
14.5.2 (1) states that member function templates of class templates
may be defined outside of their class template definition. However it
only provides an example where the member function template has a
function parameter whose type matches the template parameter. No
explicit mention is made of whether such an out of line definition is
allowed when a member function's template arguments cannot be deduced
from function arguments.

In my opinion the following syntax should be allowed:

template <typename T> struct A {
    template <typename U> void f();
};

template <typename T> template <typename U>
void A<T>::f<U>() {
}

int main() {
    A<int> a;
    a.f<double>();
}

This syntax is accepted by one out of the four compilers I tested.

Am I missing something? Should I rephrase this message as a defect
report?

Cheers,
Nicola Musatti

---
[ 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://www.comeaucomputing.com/csc/faq.html                      ]





Author: Nicola Musatti <nicola.musatti@gmail.com>
Date: Fri, 25 May 2007 11:17:02 CST
Raw View
On May 25, 4:25 am, gre...@pacbell.net (Greg Herlihy) wrote:
> On 5/24/07 12:39 PM, in article
> 1180001745.029006.204...@m36g2000hse.googlegroups.com, "Nicola Musatti"
>
> <nicola.musa...@gmail.com> wrote:
[...]
> According to the paragraph cited then, it is legal for any member function
> template - including one with non-deducible template type parameters - to be
> defined outside of its class definition.

You are right and as it often happens I realized it soon after
posting. I was misled by less than clear compiler error messages...

> > In my opinion the following syntax should be allowed:
>
> > template <typename T> struct A {
> >     template <typename U> void f();
> > };
>
> > template <typename T> template <typename U>
> > void A<T>::f<U>() {
> > }

.which pointed to the A<T>:: part of the above definition.

> No, f<U> would signify some kind of specialization of the f<> member
> template - and not its general definition. The correct definition of f()
> would be:
>
>      template <typename T> template <typename U>
>      void A<T>::f() {
>      }

Which is actually accepted by all the compilers I have available.

> > This syntax is accepted by one out of the four compilers I tested.
>
> It should not have been accepted by any C++ compiler.

Indeed.

Cheers,
Nicola Musatti

---
[ 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://www.comeaucomputing.com/csc/faq.html                      ]