conflict between friend function and inherited class function

C

ciccio

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>;
===================================
 
V

Victor Bazarov

ciccio said:
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
 
C

ciccio

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 said:
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?
 
V

Victor Bazarov

ciccio said:
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.
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


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
 
C

ciccio

What do you mean with gnu extensions? Could you elaborate a bit on
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top