template parameters cannot be friends

Discussion in 'C++' started by siddhu, Apr 18, 2007.

  1. siddhu

    siddhu Guest

    Hello,
    would you suggest to me, why gcc 3.3.3 can not compile this:

    template<class T>
    class Base
    {
    Base(){}
    friend T;
    };

    class A:virtual public Base<A>
    {
    public:
    A(){}
    };

    # g++ test.cpp
    test.cpp:6: error: template parameters cannot be friends

    while CC,MS VisualC++, gcc 2.95 do.
    Does it conflict with C++ standard?
    How can i change code to produce such behaviour? I need to use
    template
    Thank you.
     
    siddhu, Apr 18, 2007
    #1
    1. Advertising

  2. siddhu wrote:
    > Hello,
    > would you suggest to me, why gcc 3.3.3 can not compile this:
    >
    > template<class T>
    > class Base
    > {
    > Base(){}
    > friend T;
    > };
    >
    > class A:virtual public Base<A>
    > {
    > public:
    > A(){}
    > };
    >
    > # g++ test.cpp
    > test.cpp:6: error: template parameters cannot be friends
    >
    > while CC,MS VisualC++, gcc 2.95 do.
    > Does it conflict with C++ standard?


    No.

    > How can i change code to produce such behaviour? I need to use
    > template


    Make the constructor of 'Base' protected.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Apr 18, 2007
    #2
    1. Advertising

  3. siddhu

    siddhu Guest

    On Apr 18, 4:14 pm, "Victor Bazarov" <> wrote:
    > siddhu wrote:
    > > Hello,
    > > would you suggest to me, why gcc 3.3.3 can not compile this:

    >
    > > template<class T>
    > > class Base
    > > {
    > > Base(){}
    > > friend T;
    > > };

    >
    > > class A:virtual public Base<A>
    > > {
    > > public:
    > > A(){}
    > > };

    >
    > > # g++ test.cpp
    > > test.cpp:6: error: template parameters cannot be friends

    >
    > > while CC,MS VisualC++, gcc 2.95 do.
    > > Does it conflict with C++ standard?

    >
    > No.
    >
    > > How can i change code to produce such behaviour? I need to use
    > > template

    >
    > Make the constructor of 'Base' protected.
    >
    > V
    > --
    > Please remove capital 'A's when replying by e-mail
    > I do not respond to top-posted replies, please don't ask- Hide quoted text -
    >
    > - Show quoted text -


    I want class A to be final class. Thats why I have kept Base's
    constructor is private. Even if I make it protected ,it is not going
    to get compiled.
     
    siddhu, Apr 18, 2007
    #3
  4. siddhu

    red floyd Guest

    siddhu wrote:
    > On Apr 18, 4:14 pm, "Victor Bazarov" <> wrote:
    >> siddhu wrote:
    >>> Hello,
    >>> would you suggest to me, why gcc 3.3.3 can not compile this:
    >>> template<class T>
    >>> class Base
    >>> {
    >>> Base(){}
    >>> friend T;
    >>> };
    >>> class A:virtual public Base<A>
    >>> {
    >>> public:
    >>> A(){}
    >>> };
    >>> # g++ test.cpp
    >>> test.cpp:6: error: template parameters cannot be friends
    >>> while CC,MS VisualC++, gcc 2.95 do.
    >>> Does it conflict with C++ standard?

    >> No.
    >>
    >>> How can i change code to produce such behaviour? I need to use
    >>> template

    >> Make the constructor of 'Base' protected.
    >>
    >> V
    >> --
    >> Please remove capital 'A's when replying by e-mail
    >> I do not respond to top-posted replies, please don't ask- Hide quoted text -
    >>
    >> - Show quoted text -

    >
    > I want class A to be final class. Thats why I have kept Base's
    > constructor is private. Even if I make it protected ,it is not going
    > to get compiled.


    This is why you say what you *want* to do. Your question is a FAQ.

    http://www.parashift.com/c -faq-lite/strange-inheritance.html#faq-23.11
     
    red floyd, Apr 18, 2007
    #4
  5. siddhu

    red floyd Guest

    siddhu wrote:
    >[redacted]
    >
    > I want class A to be final class. Thats why I have kept Base's
    > constructor is private. Even if I make it protected ,it is not going
    > to get compiled.
    >


    Sorry, I phrased that poorly. This is why you should explain *what* the
    problem that you're trying to solve is, rather than *how* you're trying
    to solve it.

    Once we knew what you were attempting to do, I was able to point you to
    the FAQ.
     
    red floyd, Apr 18, 2007
    #5
  6. siddhu

    mlimber Guest

    mlimber, Apr 18, 2007
    #6
  7. siddhu

    siddhu Guest

    On Apr 18, 4:33 pm, red floyd <> wrote:
    > siddhu wrote:
    > >[redacted]

    >
    > > I want class A to be final class. Thats why I have kept Base's
    > > constructor is private. Even if I make it protected ,it is not going
    > > to get compiled.

    >
    > Sorry, I phrased that poorly. This is why you should explain *what* the
    > problem that you're trying to solve is, rather than *how* you're trying
    > to solve it.
    >
    > Once we knew what you were attempting to do, I was able to point you to
    > the FAQ.


    I know how to create final class using non-template base class.

    This is how i did that:

    class Base
    {
    private:
    Base(){}
    friend class A;

    };

    class A:virtual public Base
    {
    public:
    A(){}
    };

    But I want it to be done using template base class so that it can be
    reused to create other final classes.

    So I did it in the following way:

    template<class T>
    class Base
    {
    Base(){}
    friend T;



    };


    class A:virtual public Base<A>
    {
    public:
    A(){}


    };

    But it is not getting compiled on gcc 3.3.3.

    # g++ test.cpp
    test.cpp:6: error: template parameters cannot be friends

    while CC,MS VisualC++, gcc 2.95 do.

    So how can I achieve this using template base class?
     
    siddhu, Apr 18, 2007
    #7
  8. siddhu wrote:
    > On Apr 18, 4:33 pm, red floyd <> wrote:
    >> siddhu wrote:
    >>> [redacted]

    >>
    >>> I want class A to be final class. Thats why I have kept Base's
    >>> constructor is private. Even if I make it protected ,it is not going
    >>> to get compiled.

    >>
    >> Sorry, I phrased that poorly. This is why you should explain *what*
    >> the problem that you're trying to solve is, rather than *how* you're
    >> trying to solve it.
    >>
    >> Once we knew what you were attempting to do, I was able to point you
    >> to the FAQ.

    >
    > I know how to create final class using non-template base class.
    >
    > This is how i did that:
    >
    > class Base
    > {
    > private:
    > Base(){}
    > friend class A;
    >
    > };
    >
    > class A:virtual public Base
    > {
    > public:
    > A(){}
    > };
    >
    > But I want it to be done using template base class so that it can be
    > reused to create other final classes.
    >
    > So I did it in the following way:
    >
    > template<class T>
    > class Base
    > {
    > Base(){}
    > friend T;


    That's a syntax error. You need 'class' there. But don't bother,
    it's not going to work.

    >
    >
    >
    > };
    >
    >
    > class A:virtual public Base<A>
    > {
    > public:
    > A(){}
    >
    >
    > };
    >
    > But it is not getting compiled on gcc 3.3.3.


    And it shouldn't compile on any compliant compiler.

    >
    > # g++ test.cpp
    > test.cpp:6: error: template parameters cannot be friends
    >
    > while CC,MS VisualC++, gcc 2.95 do.


    All of which are non-compliant in this case.

    >
    > So how can I achieve this using template base class?


    There is no way. You are not allowed to make a template argument
    a friend. That's all.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Apr 18, 2007
    #8
  9. siddhu

    dragoncoder Guest

    > > So how can I achieve this using template base class?
    >
    > There is no way. You are not allowed to make a template argument
    > a friend. That's all.
    >

    Could you please quote the lines from the standard which enforce this?

    Thanks.
     
    dragoncoder, Apr 18, 2007
    #9
  10. siddhu wrote:
    ....
    >
    > So I did it in the following way:
    >
    > template<class T>
    > class Base
    > {
    > Base(){}
    > friend T;
    >
    >
    >
    > };
    >
    >
    > class A:virtual public Base<A>
    > {
    > public:
    > A(){}
    >
    >
    > };
    >
    > But it is not getting compiled on gcc 3.3.3.
    >
    > # g++ test.cpp
    > test.cpp:6: error: template parameters cannot be friends
    >
    > while CC,MS VisualC++, gcc 2.95 do.
    >
    > So how can I achieve this using template base class?
    >
    >


    It compiles in comeau C++ as well.

    The gcc 4.3 compiler complains as well.

    xxxa.cpp:7: error: a class-key must be used when declaring a friend
    xxxa.cpp:7: error: friend declaration does not name a class or function



    I tried this... The compilers complained about this ...

    template<class T>
    class Base
    {
    Base(){}
    friend T::T();
    };

    class A:virtual public Base<A>
    {
    public:
    A(){}
    };

    Which begs the question, how do you make a friend constructor ?
     
    Gianni Mariani, Apr 18, 2007
    #10
  11. siddhu

    red floyd Guest

    dragoncoder wrote:
    >>> So how can I achieve this using template base class?

    >> There is no way. You are not allowed to make a template argument
    >> a friend. That's all.
    >>

    > Could you please quote the lines from the standard which enforce this?
    >
    >


    Actually, I'm interested, too. I couldn't find it in 14.5.3 or in 14.6.5.
     
    red floyd, Apr 18, 2007
    #11
  12. dragoncoder wrote:
    >>> So how can I achieve this using template base class?

    >>
    >> There is no way. You are not allowed to make a template argument
    >> a friend. That's all.
    >>

    > Could you please quote the lines from the standard which enforce this?


    Subclause 7.1.5.3, paragraph 2.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Apr 18, 2007
    #12
  13. siddhu

    Mark P Guest

    red floyd wrote:
    > dragoncoder wrote:
    >>>> So how can I achieve this using template base class?
    >>> There is no way. You are not allowed to make a template argument
    >>> a friend. That's all.
    >>>

    >> Could you please quote the lines from the standard which enforce this?
    >>
    >>

    >
    > Actually, I'm interested, too. I couldn't find it in 14.5.3 or in 14.6.5.


    Try 7.1.5.3 sec. 2

    Mark
     
    Mark P, Apr 19, 2007
    #13
    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. Asfand Yar Qazi
    Replies:
    5
    Views:
    370
    Siemel Naran
    Apr 7, 2004
  2. Replies:
    2
    Views:
    607
  3. John Harrison

    template parameters as friends

    John Harrison, Nov 24, 2005, in forum: C++
    Replies:
    5
    Views:
    336
  4. Dave Rudolf
    Replies:
    2
    Views:
    493
    Dave the Funkatron
    Mar 9, 2007
  5. Replies:
    0
    Views:
    667
Loading...

Share This Page