class template member function - compilation error

Discussion in 'C++' started by subramanian100in@yahoo.com, India, Nov 2, 2007.

  1. , India

    , India Guest

    consider the following program

    #include <iostream>

    using namespace std;

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

    private:
    int val;
    };

    template <class T> class Test
    {
    private:
    T t;

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

    int main( )
    {
    Test<Rec> r;

    // 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<Rec> itself?. Does
    it mean the that the template member function definition is generated
    only when the corresponding function is used(Test<T>::print( ) in this
    case) ?

    Kindly explain.

    Thanks
    V.Subramanian
    , India, Nov 2, 2007
    #1
    1. Advertising

  2. On Nov 2, 11:33 am, ", India"
    <> wrote:
    > consider the following program
    >
    > #include <iostream>
    >
    > using namespace std;
    >
    > class Rec
    > {
    > public:
    > Rec(int arg = 10) : val(arg) { }
    >
    > private:
    > int val;
    >
    > };
    >
    > template <class T> class Test
    > {
    > private:
    > T t;
    >
    > public:
    > void print( ) const { cout << t << endl; }
    >
    > };
    >
    > int main( )
    > {
    > Test<Rec> r;
    >
    > // 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<Rec> itself?. Does
    > it mean the that the template member function definition is generated
    > only when the corresponding function is used(Test<T>::print( ) 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
    Neelesh Bodas, Nov 2, 2007
    #2
    1. Advertising

  3. , India

    Guest

    On 2 Nov, 08:57, Neelesh Bodas <> wrote:
    > On Nov 2, 11:33 am, ", India"
    >
    >
    >
    > <> wrote:
    > > consider the following program

    >
    > > #include <iostream>

    >
    > > using namespace std;

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

    >
    > > private:
    > > int val;

    >
    > > };

    >
    > > template <class T> class Test
    > > {
    > > private:
    > > T t;

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

    >
    > > };

    >
    > > int main( )
    > > {
    > > Test<Rec> r;

    >
    > > // 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<Rec> itself?. Does
    > > it mean the that the template member function definition is generated
    > > only when the corresponding function is used(Test<T>::print( ) 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
    , Nov 2, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dave
    Replies:
    4
    Views:
    7,645
    pdixtl
    Jun 4, 2010
  2. Brian Riis
    Replies:
    3
    Views:
    484
    Brian Riis
    Nov 2, 2004
  3. Replies:
    1
    Views:
    467
    Victor Bazarov
    Jul 20, 2005
  4. sunil
    Replies:
    1
    Views:
    365
    Ian Collins
    Aug 15, 2008
  5. Peng Yu
    Replies:
    3
    Views:
    754
    Thomas J. Gritzan
    Oct 26, 2008
Loading...

Share This Page