conflict between friend function and inherited class function

Discussion in 'C++' started by ciccio, Jan 17, 2008.

  1. ciccio

    ciccio Guest

    Dear all, once again I stumbled upon the following puzzling problem.

    When having the following two files (see below), the gnu compiler
    compiles the file without a problem while the compiler complains about
    the fact that the function foo (which is declared as a template) is not
    a template! The problem itself vanishes when changing the name of the
    function foo in the class bar into some other function name, lets say goo.

    The problem apears to originate from the same function name which excist
    in the parent class.

    So my question is now, is this syntax correct, or is one of the
    compilers failing?

    Thanks for the help


    [ testing]$ g++ -c car.cpp
    [ testing]$ icpc -c car.cpp
    vector.hpp(13): error: foo is not a template
    friend void foo <> (car<T> &, car<T> &);
    ^
    detected during instantiation of class "car<T> [with T=int]"
    at line 2 of "car.cpp"

    compilation aborted for vector.cpp (code 2)


    ======== car.hpp ========
    #ifndef CAR_HPP
    #define CAR_HPP

    template<typename T> class bar {
    public :
    void foo(bar<T> &); // not working
    // void goo(bar<T> &); THIS ONE WOULD WORK
    };

    template<typename T> class car;
    template<typename T> void foo(car<T> &, car<T> &);

    template<typename T> class car : public bar<T> {
    friend void foo <> (car<T> &, car<T> &);
    };
    #endif
    ============= car.cpp =============
    #include "car.hpp"
    template class car<int>;
    ===================================
     
    ciccio, Jan 17, 2008
    #1
    1. Advertising

  2. ciccio wrote:
    > Dear all, once again I stumbled upon the following puzzling problem.
    >
    > When having the following two files (see below), the gnu compiler
    > compiles the file without a problem while the compiler


    Sorry, which one?

    > complains about
    > the fact that the function foo (which is declared as a template) is
    > not
    > a template! The problem itself vanishes when changing the name of the
    > function foo in the class bar into some other function name, lets say
    > goo.
    >
    > The problem apears to originate from the same function name which
    > excist in the parent class.
    >
    > So my question is now, is this syntax correct, or is one of the
    > compilers failing?


    They both can be failing.

    >
    > Thanks for the help
    >
    >
    > [ testing]$ g++ -c car.cpp


    To verify your code better, you need to make the compilation _strict_
    and _conforming_. Here you're just letting GNU extensions loose.

    > [ testing]$ icpc -c car.cpp
    > vector.hpp(13): error: foo is not a template
    > friend void foo <> (car<T> &, car<T> &);
    > ^
    > detected during instantiation of class "car<T> [with T=int]"
    > at line 2 of "car.cpp"
    >
    > compilation aborted for vector.cpp (code 2)
    >
    >
    > ======== car.hpp ========
    > #ifndef CAR_HPP
    > #define CAR_HPP
    >
    > template<typename T> class bar {
    > public :
    > void foo(bar<T> &); // not working
    > // void goo(bar<T> &); THIS ONE WOULD WORK
    > };
    >
    > template<typename T> class car;
    > template<typename T> void foo(car<T> &, car<T> &);
    >
    > template<typename T> class car : public bar<T> {
    > friend void foo <> (car<T> &, car<T> &);


    If youi need a particular instantiation of 'foo' to be the friend,
    you need to give it the template arguments, I believe

    friend void foo<T>(car<T>&, car<T>&);

    Have you tried that?

    > };
    > #endif
    > ============= car.cpp =============
    > #include "car.hpp"
    > template class car<int>;
    > ===================================


    So, have you actually tried inserting the entire 'car.hpp' into
    'car.cpp' and then posting it here. It doesn't matter really that
    you have two files, does it?

    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, Jan 17, 2008
    #2
    1. Advertising

  3. ciccio

    ciccio Guest

    <snip>

    > To verify your code better, you need to make the compilation _strict_
    > and _conforming_. Here you're just letting GNU extensions loose.
    >


    What do you mean with gnu extensions? Could you elaborate a bit on this?

    > If youi need a particular instantiation of 'foo' to be the friend,
    > you need to give it the template arguments, I believe
    >
    > friend void foo<T>(car<T>&, car<T>&);
    >
    > Have you tried that?


    Yep, it gives the same error. And the above rule is given in faq-lite
    also. When I remove the <> from the friend declaration, i.e.
    friend void foo(car <T> &, car<T> &);
    The intel compiler gives a warning saying that I might need to include
    the <> .

    > So, have you actually tried inserting the entire 'car.hpp' into
    > 'car.cpp' and then posting it here. It doesn't matter really that
    > you have two files, does it?


    Sorry, you are right there, here is the one file example

    === START CAR.CPP ======
    template<typename T> class bar {
    public :
    void foo(bar<T> &);
    };

    template<typename T> class car;
    template<typename T> void foo(car<T> &, car<T> &);

    template<typename T> class car : public bar<T> {
    friend void foo <T> (car<T> &, car<T> &);
    };

    car<int> a;

    ===== END CAR.CPP =====

    Again the same question remains, why does foo from the class bar
    conflicts with the friend definition of the template function foo in the
    class car?
     
    ciccio, Jan 17, 2008
    #3
  4. ciccio wrote:
    > <snip>
    >
    >> To verify your code better, you need to make the compilation _strict_
    >> and _conforming_. Here you're just letting GNU extensions loose.
    >>

    >
    > What do you mean with gnu extensions? Could you elaborate a bit on
    > this?


    Not sure what to tell you. GNU C++ is a slightly different language
    than standard C++. It's full of extensions, some of which are bugs
    in the compiler or in the compiler creators' understanding of the
    Standard. You need to disable those. See man page to find out what
    command-line switches to use.

    >
    >> If youi need a particular instantiation of 'foo' to be the friend,
    >> you need to give it the template arguments, I believe
    >>
    >> friend void foo<T>(car<T>&, car<T>&);
    >>
    >> Have you tried that?

    >
    > Yep, it gives the same error. And the above rule is given in faq-lite
    > also. When I remove the <> from the friend declaration, i.e.
    > friend void foo(car <T> &, car<T> &);
    > The intel compiler gives a warning saying that I might need to include
    > the <> .
    >
    >> So, have you actually tried inserting the entire 'car.hpp' into
    >> 'car.cpp' and then posting it here. It doesn't matter really that
    >> you have two files, does it?

    >
    > Sorry, you are right there, here is the one file example
    >
    > === START CAR.CPP ======
    > template<typename T> class bar {
    > public :
    > void foo(bar<T> &);
    > };
    >
    > template<typename T> class car;
    > template<typename T> void foo(car<T> &, car<T> &);
    >
    > template<typename T> class car : public bar<T> {
    > friend void foo <T> (car<T> &, car<T> &);
    > };
    >
    > car<int> a;
    >
    > ===== END CAR.CPP =====
    >
    > Again the same question remains, why does foo from the class bar
    > conflicts with the friend definition of the template function foo in
    > the class car?


    The code you posted here compiles without a problem with Comeau online.

    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, Jan 17, 2008
    #4
  5. ciccio

    ciccio Guest

    >> What do you mean with gnu extensions? Could you elaborate a bit on
    >> this?

    >
    > Not sure what to tell you. GNU C++ is a slightly different language
    > than standard C++. It's full of extensions, some of which are bugs
    > in the compiler or in the compiler creators' understanding of the
    > Standard. You need to disable those. See man page to find out what
    > command-line switches to use.


    I have switched those extensions off and the code compiles still fine in
    GNU C++. I have done the same with the intel compiler but there it
    gives that error again. Nonetheless the intel compiler has an other
    option called -strict-ansi, with this it is possible to compile, but the
    code becomes much slower due to this and is not really acceptable.

    And since the intel compiler bases itself on the GNU compiler, I am a
    bit surprised by its error.
     
    ciccio, Jan 18, 2008
    #5
    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. Ganesh
    Replies:
    3
    Views:
    503
    Rolf Magnus
    May 10, 2005
  2. charles cashion

    css conflict (or html conflict)

    charles cashion, Feb 18, 2009, in forum: HTML
    Replies:
    2
    Views:
    799
    charles cashion
    Feb 18, 2009
  3. Andrea Crotti

    Friend functions not inherited

    Andrea Crotti, Nov 4, 2010, in forum: C++
    Replies:
    7
    Views:
    724
    Andrea Crotti
    Nov 5, 2010
  4. 7stud --
    Replies:
    11
    Views:
    414
    7stud --
    Nov 9, 2007
  5. Peter
    Replies:
    2
    Views:
    278
    Öö Tiib
    Jun 6, 2013
Loading...

Share This Page