|
Archives
D Programming
DD.gnu digitalmars.D digitalmars.D.bugs digitalmars.D.dtl digitalmars.D.dwt digitalmars.D.announce digitalmars.D.learn digitalmars.D.debugger C/C++ Programming
c++c++.announce c++.atl c++.beta c++.chat c++.command-line c++.dos c++.dos.16-bits c++.dos.32-bits c++.idde c++.mfc c++.rtl c++.stl c++.stl.hp c++.stl.port c++.stl.sgi c++.stlsoft c++.windows c++.windows.16-bits c++.windows.32-bits c++.wxwindows digitalmars.empire digitalmars.DMDScript |
c++ - dummy variable has to match?
The program below compiles fine, as long as the definition of fun2 uses the
same "template dummy variable" used in the declaration.
Member function fun1 has no such restriction. Why is this? I ask because
sometimes inquiring into mysteries pays off with unexpected
valuable knowledge.
#include <iostream.h>
template <int kk> //dummy variable is kk
class test {
int val;
public:
test() {val=0;};
int fun1();
template <int rr> //dummy variable is rr
friend int fun2(test<rr> a);
};
template <int ss> //dummy variable is ss
int test<ss>::fun1() {return val+ss;};
template <int rr> //dummy variable has to be rr?!
int fun2(test<rr> a) {return a.val+rr;};
int main()
{
test<5> a;
cout << a.fun1() << '\n'; //test member function
cout << fun2(a) << '\n'; //test friend function
}
Sep 05 2003
Try using a reference instead. Sep 07 2003
Experiment shows that passing the argument to fun2 by value or reference makes no difference - the "template dummy variable" still has to match. Sep 07 2003
Experiment shows that passing the argument to fun2 by value or reference makes no difference - the "template dummy variable" still has to match. Sep 07 2003
|