Topic: Template Functions as Friends of Template Classes


Author: wmm@fastdial.net
Date: 1999/11/28
Raw View
In article <81eh9p$rld$1@mailusr.wdf.sap-ag.de>,
  "Gregor Frey" <gregor.karl.frey@sap.com> wrote:
> Hi,
> the gcc and the Visual-C++ Compiler do not agree on how to define a
template
> function as a friend of a template class.
> Look at the following coding:
>
> -------------- snip ------------------
> template <class T>
> void foo(T t);
>
> template <class T>
> class Test{
>   friend void foo(T t);                 // required by VisualC++, but
> rejected by g++
>   // friend void foo<>(T t);          // required by g++, but
rejected by
> VisualC++
> };
> -------------------- snip ------------------------
>
> Which way is right? Which conforms to the standard?

According to 14.5.3p1, if the name of the friend is neither a
template-id (i.e., a template-name followed by a template argument
list) nor a qualified-id, the friend declaration declares a non-
template function.  That is, your "friend void foo(T t);" is a
non-template function that just happens to have the same name as the
template function "foo" but is a different function.  Only the second
form "friend void foo<>(T t);" declares that "foo<T>(T)" is a friend.

--
William M. Miller, wmm@fastdial.net
OnDisplay, Inc. (www.ondisplay.com)


Sent via Deja.com http://www.deja.com/
Before you buy.


      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]

[ 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: "Gregor Frey" <gregor.karl.frey@sap.com>
Date: 1999/11/25
Raw View
Hi,
the gcc and the Visual-C++ Compiler do not agree on how to define a template
function as a friend of a template class.
Look at the following coding:

-------------- snip ------------------
using namespace std;

template <class T>
void foo(T t);

template <class T>
class Test{
  friend void foo(T t);                 // required by VisualC++, but
rejected by g++
  // friend void foo<>(T t);          // required by g++, but rejected by
VisualC++
private:
  static T m_t;
public:
  static void show() {
    cout << "m_t: " << m_t << endl;
  }
};

template <class T>
T Test<T>::m_t;

template <class T>
void foo(T t) {
  Test<T>::m_t = t;
}

int main() {
  foo(25);
  foo(3.45);
  Test<int>::show();
  Test<double>::show();
  return 0;
}
-------------------- snip ------------------------

Which way is right? Which conforms to the standard?

Gregor Frey



      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]

[ 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              ]