template partial specialization

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chandra Shekhar Kumar

    #16
    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

    Comment

    • Victor Bazarov

      #17
      Re: template partial specialization

      "Chandra Shekhar Kumar" <chandra.kumar@ oracle.com> wrote...[color=blue][color=green]
      > >[color=darkred]
      > > > #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=darkred]
      > > > << 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[/color]
      well...[color=blue]
      > 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[/color]
      my[color=blue]
      > next posting of the code(pl forgive me for missing the return types , i[/color]
      meant[color=blue]
      > 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 << ":"[/color]
      << b[color=blue]
      > <<
      > endl;}[/color]

      This is not a partial specialisation of 'f'. It's another template
      called 'f' that has one template argument. That's known as "function
      overloading" (please see subclause 14.8.3). A partial specialisation
      (if it were allowed) _would_ look like this:

      template<class T1> void f<T1,int>(T1 a, int b) ...

      Victor


      Comment

      • tom_usenet

        #18
        Re: template partial specialization

        On Wed, 25 Jun 2003 06:46:41 +0530, Chandra Shekhar Kumar
        <chandra.kumar@ oracle.com> wrote:
        [color=blue]
        >so , now the question is when to choose function templates, and when function
        >overloading... ..
        >the limitation of function templates is that they donot scale well.
        >what i mean is: with the function templates having 2 or more arguments cann't
        >be fully specilaized.
        >
        >see the code below:
        >
        >#include <iostream>
        >#include <map>
        >
        >using namespace std;
        >
        >template<cla ss T1, class T2> f(T1 a, T2 b) { cout << a << ":" << b << endl;}[/color]

        Where is the return type? The code is illegal, and if your compiler
        compiles it, your compiler is non-standard.
        [color=blue]
        >
        >//partial specialization of f......
        >
        >template<cla ss T1> f(T1 a, int b) { cout << "partial::" << a << ":" << b <<
        >endl;}[/color]

        That is an overload. Why do you think it is a partial specialization?
        [color=blue]
        >
        >
        >//fully specialisation of f.....not legal code...so commented.....
        >
        >//template<> f(int a, int b) { cout << "fully:::" << a << ":" << b << endl;}[/color]

        That is indeed a full specialization (again, missing the return type).
        It would be legal code if you put in the argument list, and thus told
        the compiler which overload you were specializing:

        template<> void f<int>(int a, int b) { cout << "fully:::" << a << ":"
        << b << endl;}

        (note the <int> bit)
        [color=blue]
        >template <class T> g(T a) { cout << "g:: " << a << endl;}
        >
        >//fully specialisation of g.....legal here coz g takes only one argument.....
        >
        >template<> g(int a) { cout << "fully::g:: :" << a << endl;}[/color]

        Right, but where is the return type? Where did you learn your
        "dialect" of C++!?
        [color=blue]
        >
        >template<cla ss T> h(T a) { cout << "h::" << a << endl; }
        >
        >//fully specialisation. ..same as above....:-)))
        >
        >template<> template<class T1, class T2> h(pair<T1, T2>& p) { cout << "pair::"
        ><< p.first << ":::" << p.second << endl;}[/color]

        That is illegal code, and your compiler appears to be broken.

        Please don't form an opinion of what and what isn't legal code based
        on an old, pre-standard compiler, and please don't misinform readers
        by posting complete rubbish.

        Tom

        Comment

        Working...