Problem with defining template friend function of a template class.

Discussion in 'C++' started by PengYu.UT@gmail.com, Nov 9, 2005.

  1. Guest

    The following program works with g++3.3. I'm assuming g++-3.4 is more
    standard conforming that g++-3.3. Would you please tell me what is
    right way to declare a template friend function of a template class?

    Thanks,
    Peng

    #include <iostream>

    template <typename T>
    class A{
    private:
    A(){}
    T _a;
    public:
    friend A *makeA<T>();//error in g++-3.4, works with g++3.3
    };

    template <typename T>
    A<T> *makeA(){
    return new A<T>;
    }

    int main ( void ) {
    makeA<int>();
    }
     
    , Nov 9, 2005
    #1
    1. Advertising

  2. Neelesh Guest

    wrote:
    > #include <iostream>
    >
    > template <typename T>
    > class A{
    > private:
    > A(){}
    > T _a;
    > public:
    > friend A *makeA<T>();//error in g++-3.4, works with g++3.3
    > };
    >
    > template <typename T>
    > A<T> *makeA(){
    > return new A<T>;
    > }
    >
    > int main ( void ) {
    > makeA<int>();
    > }



    The C++ standard states that if a template function is a friend of a
    class then the function must be declared before the class definition.

    Hence for this code to compile, you will need to add following two
    lines before the class definition :

    template <class T> class A; // declare the class since it is used in
    function's prototype
    template<class T> A<T>* makeA(); // declare the function

    This would solve the problem.
     
    Neelesh, Nov 9, 2005
    #2
    1. Advertising

  3. Re: Problem with defining template friend function of a templateclass.

    Neelesh wrote:
    > wrote:
    >
    >>#include <iostream>
    >>
    >>template <typename T>
    >>class A{
    >> private:
    >> A(){}
    >> T _a;
    >> public:
    >> friend A *makeA<T>();//error in g++-3.4, works with g++3.3
    >>};
    >>
    >>template <typename T>
    >>A<T> *makeA(){
    >> return new A<T>;
    >>}
    >>
    >>int main ( void ) {
    >> makeA<int>();
    >>}

    >
    >
    >
    > The C++ standard states that if a template function is a friend of a
    > class then the function must be declared before the class definition.
    >
    > Hence for this code to compile, you will need to add following two
    > lines before the class definition :
    >
    > template <class T> class A; // declare the class since it is used in
    > function's prototype
    > template<class T> A<T>* makeA(); // declare the function
    >
    > This would solve the problem.
    >


    I don't know about gcc 3.4 but it's a good idea on some compilers to use
    this very similar version

    template <class T> class A;
    template <class T> A<T>* makeA();

    template <typename T>
    class A
    {
    friend A* makeA<>();
    };

    Note that <> has replaced <T> in the friend declaration.

    john
     
    John Harrison, Nov 9, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Yueh-Wei Hu
    Replies:
    0
    Views:
    472
    Yueh-Wei Hu
    May 23, 2004
  2. =?gb2312?B?wfXquw==?=
    Replies:
    10
    Views:
    715
    Victor Bazarov
    Aug 1, 2007
  3. Replies:
    2
    Views:
    698
    Triple-DES
    Feb 26, 2008
  4. A L
    Replies:
    1
    Views:
    529
    Alf P. Steinbach /Usenet
    Aug 25, 2010
  5. Peter
    Replies:
    2
    Views:
    310
    Öö Tiib
    Jun 6, 2013
Loading...

Share This Page