Error : in Template Code

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Pallav singh

    #1

    Error : in Template Code

    Hi All,

    i am getting error during explicit function Instantiation for a class
    Template
    if i do explicit Instantiation of class it work and all function
    symbol i get in object file
    But if i try to expose only one function of my class its failing

    #include <iostream>

    template<typena me T>
    class A
    {
    const T max(const T a,const T b)
    { return ((a<b)?b:a); }

    const T min(const T a,const T b)
    { return ((a<b)?a:b); }
    };

    // used for Explicit Instantiation of class
    //template class A<int>;

    // used for Explicit Instantiation of different function of class
    template int A<int>::max(int ,int);
    template int A<int>::min(int ,int);

    Thanks
    Pallav Singh
  • tatmasi@gmail.com

    #2
    Re: Error : in Template Code

    On Sep 20, 6:32 am, Pallav singh <singh.pal...@g mail.comwrote:
    Hi All,
    >
    i am getting  error during explicit function Instantiation for a class
    Template
    if i do explicit Instantiation of class it work and all function
    symbol i get in object file
    But if i try to expose only one function of my class its failing
    >
    #include <iostream>
    >
    template<typena me T>
    class A
    {
       const T  max(const T  a,const T  b)
         {  return ((a<b)?b:a);  }
    >
       const T  min(const T  a,const T  b)
        {  return ((a<b)?a:b);  }
    >
    };
    >
    // used for Explicit Instantiation of class
    //template class A<int>;
    >
    // used for Explicit Instantiation of different function of class
    template int A<int>::max(int ,int);
    template int A<int>::min(int ,int);
    >
    Thanks
    Pallav Singh
    Hi,
    I could not understand why you need explicit function templates in
    this context.
    Since the explicit instantiation of class allows you to use functions
    correctly.
    By explicit declaration functions as you done will result error
    because when a function template
    is used in a way that triggers its instantiation, a compiler will (at
    some point) need to
    see that template's definition. This breaks the usual compile and
    link distinction for
    ordinary functions, when the declaration of a function is sufficient
    to compile its use.

    Comment

    • Pallav singh

      #3
      Re: Error : in Template Code

      On Sep 20, 8:17 pm, tatm...@gmail.c om wrote:
      On Sep 20, 6:32 am, Pallav singh <singh.pal...@g mail.comwrote:
      >
      >
      >
      Hi All,
      >
      i am getting  error during explicit function Instantiation for a class
      Template
      if i do explicit Instantiation of class it work and all function
      symbol i get in object file
      But if i try to expose only one function of my class its failing
      >
      #include <iostream>
      >
      template<typena me T>
      class A
      {
         const T  max(const T  a,const T  b)
           {  return ((a<b)?b:a);  }
      >
         const T  min(const T  a,const T  b)
          {  return ((a<b)?a:b);  }
      >
      };
      >
      // used for Explicit Instantiation of class
      //template class A<int>;
      >
      // used for Explicit Instantiation of different function of class
      template int A<int>::max(int ,int);
      template int A<int>::min(int ,int);
      >
      Thanks
      Pallav Singh
      >
      Hi,
      I could not understand why you need explicit function templates in
      this context.
      Since the explicit instantiation of class allows you to use functions
      correctly.
      By explicit declaration functions as you done will result error
      because when a function template
       is used in a way that triggers its instantiation, a compiler will (at
      some point) need to
       see that template's definition. This breaks the usual compile and
      link distinction for
       ordinary functions, when the declaration of a function is sufficient
      to compile its use.
      +++++++++++++++ +++++++++++++++ +++++++++++++++ +++++++++++++++ +++++

      Hi

      But if i write code in following way then also i get Error
      during Function Template Instantiation

      Kindly let me know How to make it work ??

      #include <iostream>


      template<typena me T>
      const T max(const T a, const T b)
      { return ((a<b)?b:a); }


      template<typena me T>
      const T min(const T a, const T b)
      { return ((a<b)?a:b); }


      template int max(int,int);
      template int min(int,int);

      Comment

      • Lars Tetzlaff

        #4
        Re: Error : in Template Code

        Pallav singh schrieb:
        On Sep 20, 8:17 pm, tatm...@gmail.c om wrote:
        >On Sep 20, 6:32 am, Pallav singh <singh.pal...@g mail.comwrote:
        >>
        >>
        >>
        >
        Hi
        >
        But if i write code in following way then also i get Error
        during Function Template Instantiation
        >
        Kindly let me know How to make it work ??
        >
        #include <iostream>
        >
        >
        template<typena me T>
        const T max(const T a, const T b)
        { return ((a<b)?b:a); }
        >
        >
        template<typena me T>
        const T min(const T a, const T b)
        { return ((a<b)?a:b); }
        >
        >
        template int max(int,int);
        template int min(int,int);
        1) Wy don't you post the error message? How should we know *WHAT* error
        you get?

        2) do you mean:

        #include <iostream>


        template<typena me T>
        const T max(const T a, const T b)
        { return ((a<b)?b:a); }


        template<typena me T>
        const T min(const T a, const T b)
        { return ((a<b)?a:b); }


        template<const int max(const int, const int);
        template<const int min(const int, const int);



        Lars

        Comment

        • Ian Collins

          #5
          Re: Error : in Template Code

          Pallav singh wrote:
          >
          Hi
          >
          But if i write code in following way then also i get Error
          during Function Template Instantiation
          >
          Kindly let me know How to make it work ??
          >
          #include <iostream>
          >
          >
          template<typena me T>
          const T max(const T a, const T b)
          { return ((a<b)?b:a); }
          >
          >
          template<typena me T>
          const T min(const T a, const T b)
          { return ((a<b)?a:b); }
          >
          >
          template int max(int,int);
          template int min(int,int);
          Did you read my replay the last time you asked this?

          --
          Ian Collins.

          Comment

          Working...