PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > conflict between friend function and inherited class function
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
conflict between friend function and inherited class function

Réponse
 
LinkBack Outils de la discussion
Vieux 17/01/2008, 21h17   #1
ciccio
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut conflict between friend function and inherited class function

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


[ 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>;
===================================
  Réponse avec citation
Vieux 17/01/2008, 21h30   #2
Victor Bazarov
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: conflict between friend function and inherited class function

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


  Réponse avec citation
Vieux 17/01/2008, 21h43   #3
ciccio
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: conflict between friend function and inherited class function

<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?
  Réponse avec citation
Vieux 17/01/2008, 22h42   #4
Victor Bazarov
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: conflict between friend function and inherited class function

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


  Réponse avec citation
Vieux 18/01/2008, 00h13   #5
ciccio
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: conflict between friend function and inherited class function

>> 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.
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 11h13.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,15517 seconds with 13 queries