class template member function - compilation error

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • subramanian100in@yahoo.com, India

    #1

    class template member function - compilation error

    consider the following program

    #include <iostream>

    using namespace std;

    class Rec
    {
    public:
    Rec(int arg = 10) : val(arg) { }

    private:
    int val;
    };

    template <class Tclass Test
    {
    private:
    T t;

    public:
    void print( ) const { cout << t << endl; }
    };

    int main( )
    {
    Test<Recr;

    // r.print( );

    return 0;
    }

    This program compiles fine with g++ and VC++2005 Express Edition.

    However if I remove the comment in the line
    // r.print( ),
    I get compilation error because operator<<( ) is not defined for Rec.

    Why doesn't the compiler report this error when it tries to generate
    (that is, instantiate) a class declaration for Test<Recitself? . Does
    it mean the that the template member function definition is generated
    only when the corresponding function is used(Test<T>::p rint( ) in this
    case) ?

    Kindly explain.

    Thanks
    V.Subramanian

  • Neelesh Bodas

    #2
    Re: class template member function - compilation error

    On Nov 2, 11:33 am, "subramanian10. ..@yahoo.com, India"
    <subramanian10. ..@yahoo.comwro te:
    consider the following program
    >
    #include <iostream>
    >
    using namespace std;
    >
    class Rec
    {
    public:
    Rec(int arg = 10) : val(arg) { }
    >
    private:
    int val;
    >
    };
    >
    template <class Tclass Test
    {
    private:
    T t;
    >
    public:
    void print( ) const { cout << t << endl; }
    >
    };
    >
    int main( )
    {
    Test<Recr;
    >
    // r.print( );
    >
    return 0;
    >
    }
    >
    This program compiles fine with g++ and VC++2005 Express Edition.
    >
    However if I remove the comment in the line
    // r.print( ),
    I get compilation error because operator<<( ) is not defined for Rec.
    >
    Why doesn't the compiler report this error when it tries to generate
    (that is, instantiate) a class declaration for Test<Recitself? . Does
    it mean the that the template member function definition is generated
    only when the corresponding function is used(Test<T>::p rint( ) in this
    case) ?
    >
    Yes, when the template member function is non-virtual.

    14.7.1.(9) from the C++ standard: "An implementation shall not
    implicitly instantiate a function template, a member template, a
    nonvirtual member function, a member class or a static data member of
    a class template that does not require instantiation"

    -N

    Comment

    • nick4ng@googlemail.com

      #3
      Re: class template member function - compilation error

      On 2 Nov, 08:57, Neelesh Bodas <neelesh.bo...@ gmail.comwrote:
      On Nov 2, 11:33 am, "subramanian10. ..@yahoo.com, India"
      >
      >
      >
      <subramanian10. ..@yahoo.comwro te:
      consider the following program
      >
      #include <iostream>
      >
      using namespace std;
      >
      class Rec
      {
      public:
      Rec(int arg = 10) : val(arg) { }
      >
      private:
      int val;
      >
      };
      >
      template <class Tclass Test
      {
      private:
      T t;
      >
      public:
      void print( ) const { cout << t << endl; }
      >
      };
      >
      int main( )
      {
      Test<Recr;
      >
      // r.print( );
      >
      return 0;
      >
      }
      >
      This program compiles fine with g++ and VC++2005 Express Edition.
      >
      However if I remove the comment in the line
      // r.print( ),
      I get compilation error because operator<<( ) is not defined for Rec.
      >
      Why doesn't the compiler report this error when it tries to generate
      (that is, instantiate) a class declaration for Test<Recitself? . Does
      it mean the that the template member function definition is generated
      only when the corresponding function is used(Test<T>::p rint( ) in this
      case) ?
      >
      Yes, when the template member function is non-virtual.
      >
      14.7.1.(9) from the C++ standard: "An implementation shall not
      implicitly instantiate a function template, a member template, a
      nonvirtual member function, a member class or a static data member of
      a class template that does not require instantiation"
      >
      -N
      AFAIK it is not possible to define template virtual method. In general
      as you have reported:
      14.7.1.(9) from the C++ standard: "An implementation shall not
      implicitly instantiate a function template,...".
      This is a feature used for example in the policy pattern.

      Cheers
      n

      Comment

      Working...