Topic: Q: Template friends


Author: Ewert_Ahr._Electronic_GmbH@t-online.de (Gernot)
Date: 1999/04/28
Raw View
Scott Robert Ladd wrote:
>
> I've been over the Standard and can't determine which of two compiler
> behaviors is reasonable. The code in question involves a friend declaration
> of template function, as in:
>
> template <class T>
>     Array<T> Apply(const Array<T> & ia, T (* func)(T));
>
> template <class T>
>     class Array
>     {
>         // lotsa stuff
>
>     public:
>         #ifdef _MSC_VER
>             friend Array<T> Apply(const Array<T> & ia, T (* func)(T));
>         #else
>             friend Array<T> Apply<T>(const Array<T> & ia, T (* func)(T));
>         #endif
>
>         // more stuff
>     };
>
> As may be obvious from the preprocessor directives, Microsoft does not
> require the function to be qualified with a template argument, while the
> Linux egcs 1.1.2 version of gcc wants the <T>. If I use Apply<T>, Microsoft
> C++ 6.0SP2 expresses the error:
>
>     error C2143: syntax error : missing ';' before '<'
>
> Which compiler is correct?
>

In this case, Microsoft is correct. You want one special instantiation
of a function template to be the friend of a class, so that's what you
have to write: The name of the function "Apply()" *without* any template
parameters but *with* the correct parameters that you want it to accept.


[ 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: "Scott Robert Ladd" <srladd@frontier.net>
Date: 1999/04/26
Raw View
I've been over the Standard and can't determine which of two compiler
behaviors is reasonable. The code in question involves a friend declaration
of template function, as in:

template <class T>
    Array<T> Apply(const Array<T> & ia, T (* func)(T));

template <class T>
    class Array
    {
        // lotsa stuff

    public:
        #ifdef _MSC_VER
            friend Array<T> Apply(const Array<T> & ia, T (* func)(T));
        #else
            friend Array<T> Apply<T>(const Array<T> & ia, T (* func)(T));
        #endif

        // more stuff
    };

As may be obvious from the preprocessor directives, Microsoft does not
require the function to be qualified with a template argument, while the
Linux egcs 1.1.2 version of gcc wants the <T>. If I use Apply<T>, Microsoft
C++ 6.0SP2 expresses the error:

    error C2143: syntax error : missing ';' before '<'

Which compiler is correct?

A side note: gcc required that I forward-declare the template function
Apply, while Microsoft did not (although MS doesn't complain if it's there,
either).

Okay, I admit it -- I've spent too damned many years working in the Wintel
world with Microsoft's compilers and their clones. The more I work with
egcs-gcc and other compilers, the more I wonder how far Microsoft C++ is
from the Standard. See my other message in this regard...

== Scott Robert Ladd
      srladd@frontier.net
      http://www.frontier.net/~srladd
      public PGP key: http://www.frontier.net/~srladd/srlpgp.html
---
[ 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              ]