Re: template partial specialization
>[color=blue][color=green]
> > #include <iostream>
> > #include <map>
> >
> > using namespace std;
> >
> > template<class T> h(T a) { cout << "h::" << a << endl; }
> >
> > template<> template<class T1, class T2> h(pair<T1, T2>& p) { cout <<[/color]
> "pair::"[color=green]
> > << p.first << ":::" << p.second << endl;}
> >
> > int main()
> > {
> > h(20);
> > pair<int, float> p(20, 30.34);
> > h(p);
> > }[/color]
>
> This code doesn't compile. It's plain bogus. Please try to
> compile any code before you post it.[/color]
hi Victor, actually i compiled the above with g++ 2.8.1 and it ran well...
but i tried the same with Comeau too and u r right there....
it's not standard code....g++ guys are lenient i think.....
but my main point is this: function templates can be specialised ....see my
next posting of the code(pl forgive me for missing the return types , i meant
it to be void though :-))
i tried it with Comeau too, and it run....
#include <iostream>
#include <map>
using namespace std;
template<class T1, class T2> void f(T1 a, T2 b) { cout << a << ":" << b <<
endl;}
//partial specialization of f......
template<class T1> void f(T1 a, int b) { cout << "partial::" << a << ":" << b
<<
endl;}
//fully specialisation of f.....not legal code...so commented.....
//template<> f(int a, int b) { cout << "fully:::" << a << ":" << b << endl;}
template <class T> void g(T a) { cout << "g:: " << a << endl;}
//fully specialisation of g.....legal here coz g takes only one argument.....
template<> void g(int a) { cout << "fully::g:: :" << a << endl;}
int main()
{
f(5.45,4.56);
f(34.45, 3);
f(2, 3);
g(3.45);
g(2);
}
and the book i refer is "Modern C++ Design" by Andrei....
Thanks for yr input , no offense :-)))
Chandra
>[color=blue][color=green]
> > #include <iostream>
> > #include <map>
> >
> > using namespace std;
> >
> > template<class T> h(T a) { cout << "h::" << a << endl; }
> >
> > template<> template<class T1, class T2> h(pair<T1, T2>& p) { cout <<[/color]
> "pair::"[color=green]
> > << p.first << ":::" << p.second << endl;}
> >
> > int main()
> > {
> > h(20);
> > pair<int, float> p(20, 30.34);
> > h(p);
> > }[/color]
>
> This code doesn't compile. It's plain bogus. Please try to
> compile any code before you post it.[/color]
hi Victor, actually i compiled the above with g++ 2.8.1 and it ran well...
but i tried the same with Comeau too and u r right there....
it's not standard code....g++ guys are lenient i think.....
but my main point is this: function templates can be specialised ....see my
next posting of the code(pl forgive me for missing the return types , i meant
it to be void though :-))
i tried it with Comeau too, and it run....
#include <iostream>
#include <map>
using namespace std;
template<class T1, class T2> void f(T1 a, T2 b) { cout << a << ":" << b <<
endl;}
//partial specialization of f......
template<class T1> void f(T1 a, int b) { cout << "partial::" << a << ":" << b
<<
endl;}
//fully specialisation of f.....not legal code...so commented.....
//template<> f(int a, int b) { cout << "fully:::" << a << ":" << b << endl;}
template <class T> void g(T a) { cout << "g:: " << a << endl;}
//fully specialisation of g.....legal here coz g takes only one argument.....
template<> void g(int a) { cout << "fully::g:: :" << a << endl;}
int main()
{
f(5.45,4.56);
f(34.45, 3);
f(2, 3);
g(3.45);
g(2);
}
and the book i refer is "Modern C++ Design" by Andrei....
Thanks for yr input , no offense :-)))
Chandra
Comment