c++ - template friends
- Steve Strand (23/23) Feb 25 2003 Can the compiler handle template friends? The code below results in a
-
Richard Grant
(7/23)
Feb 25 2003
template
- Walter (1/1) Feb 25 2003 Looks like a compiler bug. -Walter
- Richard Grant (10/11) Feb 26 2003 I don't think so.
- Walter (6/17) Feb 26 2003 template
- Steve Strand (11/11) Feb 26 2003 Thank you Richard Grant. Your idea does fix all my template friend
- Richard Grant (11/20) Feb 26 2003 Actually, I think it says that you can do like:
- Jim Jennings (20/36) Feb 26 2003 Richard & Walter,
- Walter (11/17) Feb 27 2003 I tried this program on g++ (mingw) and Borland, and
- Jim Jennings (3/7) Feb 27 2003 OK, get ready for the posts.
- Walter (3/4) Feb 27 2003 I've got my shields up!
- Richard Grant (16/18) Feb 27 2003 This should make them all pretty happy..
Can the compiler handle template friends? The code below results in a linker error since the compiler calls the template friend function getitem() but never generates code for it. Compiling with the -XD flag makes the compiler say that vector<int>::data is not accessible (as if the friend declaration did not exist). The compiler version notes say that 8.29 implements template friends (I am using version 8.32) so I wonder if I am doing something wrong. Steve Strand template<class T> class vector { T data[4]; public: vector(T k) {for (int j=0; j<4; ++j) data[j]= k;} friend T getitem(const vector<T>& vref, int j); }; template<class T> T getitem(const vector<T>& vref, int j) {return vref.data[j];} int main() { vector<int> vint(1); int j= getitem(vint,1); }
Feb 25 2003
In article <b3gura$1jdg$1 digitaldaemon.com>, Steve Strand says...Can the compiler handle template friends? The code below results in a..template<class T> class vector { T data[4]; public: vector(T k) {for (int j=0; j<4; ++j) data[j]= k;} friend T getitem(const vector<T>& vref, int j);template <class T1> friend T1 getitem(const vector<T1>& vref, int j);}; template<class T> T getitem(const vector<T>& vref, int j)template<class T1> T1 getitem(const vector<T1>& vref, int j){return vref.data[j];} int main() { vector<int> vint(1); int j= getitem(vint,1); }Richard
Feb 25 2003
In article <b3h7nr$1oj3$2 digitaldaemon.com>, Walter says...Looks like a compiler bug. -WalterI don't think so. One declares a non-template global function that is a "friend" of the template class and the other defines a global function template - that is not a friend of the template class (14.5.3-1). I was kind of in a rush when I posted the correction.. you could actually just change the friend declaration like: template <class T> friend T getitem(const vector<T>& vref, int j); and leave everything else the same. Richard
Feb 26 2003
"Richard Grant" <fractal clark.net> wrote in message news:b3i7ul$29u2$1 digitaldaemon.com...In article <b3h7nr$1oj3$2 digitaldaemon.com>, Walter says...templateLooks like a compiler bug. -WalterI don't think so. One declares a non-template global function that is a "friend" of theclass and the other defines a global function template - that is not afriend ofthe template class (14.5.3-1). I was kind of in a rush when I posted the correction.. you could actuallyjustchange the friend declaration like: template <class T> friend T getitem(const vector<T>& vref, int j); and leave everything else the same.Thanks for the correction!
Feb 26 2003
Thank you Richard Grant. Your idea does fix all my template friend problems. The language standard seems to hold open the possibility that a non-template function could be a friend of all classes derived from a template class. I am unable to devise an example of this. Such a friend function could not take any arguments pointing to an instance of the class or refer to any static class members so what is the point of making it a friend? Even trying to bring an enum declared in the class scope into the friend function scope does not work with Digital Mars C++. Is the standard illogical or am I overlooking something? Steve Strand
Feb 26 2003
In article <b3j5lp$d3$1 digitaldaemon.com>, Steve Strand says...The language standard seems to hold open the possibility that a non-template function could be a friend of all classes derived from a template class. I am unable to devise an example of this. Such a friend function could not take any arguments pointing to an instance of the class or refer to any static class members so what is the point of making it a friend?Actually, I think it says that you can do like: friend T getitem(const vector<T>& vref, int j); And that for each specialization of the class template vector, there can be defined a getitem function that will be a friend to the specialization, but then you must define the function to match the template specialization like: int getitem(const vector<int>& vref, int j) { return 0; } But since I have not had the opportunity to use specializations in this context, I'll need to check on that.Even trying to bring an enum declared in the class scope into the friend function scope does not work with Digital Mars C++. Is the standard illogical or am I overlooking something?Got an example? Richard
Feb 26 2003
"Richard Grant" <fractal clark.net> wrote in message news:b3i7ul$29u2$1 digitaldaemon.com...In article <b3h7nr$1oj3$2 digitaldaemon.com>, Walter says...Richard & Walter, I know you are not in the business of worrying about other compilers, but I tried this program on g++ (mingw) and Borland, and neither of them would compile it. dmc does. Executing g++.exe... ./* .... clip ...*/ C:/Dev-Cpp/Worktemp/template.cpp:9: declaration of `class T' C:/Dev-Cpp/Worktemp/template.cpp:4: shadows template parm `class T' Execution terminated Borland bcc32 says this:Looks like a compiler bug. -WalterI don't think so. One declares a non-template global function that is a "friend" of the template class and the other defines a global function template - that is not a friend of the template class (14.5.3-1). I was kind of in a rush when I posted the correction.. you could actually just change the friend declaration like: template <class T> friend T getitem(const vector<T>& vref, int j); and leave everything else the same. RichardMAKE Version 5.2 Copyright (c) 1987, 2000 Borland C:\borland\bcc55\bin\BCC32 -c -q -O1 -v- -D_WINVER=0x0400 -D_WIN32_WINNT=+ 0x0400 -IC:\borland\bcc55\include -o.\template.obj .\template.cpp.\template.cpp:! Warning W8027 .\template.cpp 8: Functions containing for are not expanded * inline ! Fatal F1004 .\template.cpp 9: Internal compiler error at 0x480d6e with bas * e 0x400000 in function main() ! Fatal F1004 .\template.cpp 9: Internal compiler error in function main()** error 1 ** deleting .\template.obj---- Make had errors. Right click error line to open file. Will D have this house of mirrors called templates? Sometimes I think that the only people profiting from C++ are book authors and AWL. Jim
Feb 26 2003
"Jim Jennings" <jwjenn mindspring.com> wrote in message news:b3jhvj$8s8$1 digitaldaemon.com...Richard & Walter, I know you are not in the business of worrying about other compilers, butI tried this program on g++ (mingw) and Borland, andneither of them would compile it. dmc does.Cool!Will D have this house of mirrors called templates?It already does have it. But take a look, it has the power of C++ templates with perhaps only one tenth of the complexity, weird cases, strange interactions, etc.Sometimes I think that the only people profiting from C++ are book authorsandAWL.Templates are too powerful a feature to ignore. But I also think they should be used with great caution - most of the uses I see for it are a poor tradeoff for the maintenance grief.
Feb 27 2003
"Walter" <walter digitalmars.com> wrote in message news:b3kojl$uqg$2 digitaldaemon.com...OK, get ready for the posts. jwjWill D have this house of mirrors called templates?It already does have it. But take a look, it has the power of C++ templates with perhaps only one tenth of the complexity, weird cases, strange interactions, etc.
Feb 27 2003
"Jim Jennings" <jwjenn mindspring.com> wrote in message news:b3l6t5$190t$1 digitaldaemon.com...OK, get ready for the posts.I've got my shields up!
Feb 27 2003
In article <b3jhvj$8s8$1 digitaldaemon.com>, Jim Jennings says...I know you are not in the business of worrying about other compilers, but I tried this program on g++ (mingw) and Borland, and neither of them would compile it. dmc does.This should make them all pretty happy.. template <class T> class A; template <class U> void fn(A<U>& a); template <class T> class A { int i; template <class U> friend void fn(A<U>& a); }; template <class U> void fn(A<U>& a) { a.i = 1; } int main() { A<int> a; fn(a); } Richard
Feb 27 2003