question on class template virtual member function

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

  1. , India

    , India Guest

    #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:
    virtual void print( ) const { cout << t << endl; }

    };

    int main( )
    {
    Test<Rec> r;

    return 0;

    }

    This program does not compile with g++ and also VC++2005 Express
    Edition. However if the keyword 'virtual' is removed in
    Test<T>::print(), it compiles fine. Since 'print()' function is not at
    all called in this program, why do I get compilation error if
    Test<T>::print() is virtual ?

    Kindly explain.

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

  2. , India a écrit :
    > #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:
    > virtual void print( ) const { cout << t << endl; }
    >
    > };
    >
    > int main( )
    > {
    > Test<Rec> r;
    >
    > return 0;
    >
    > }
    >
    > This program does not compile with g++ and also VC++2005 Express
    > Edition. However if the keyword 'virtual' is removed in
    > Test<T>::print(), it compiles fine. Since 'print()' function is not at
    > all called in this program, why do I get compilation error if
    > Test<T>::print() is virtual ?


    I have not checked in the standard but my intuition is that the virtual
    function print() get instanciated in order to populate the vtable of
    Test<Rec>.

    When virtual is removed, the function is not instanciated because it is
    not used.

    When instanciated, the Test<Rec>::print() fails because
    cout<<Rec()<<endl is not defined.

    Michael
    Michael DOUBEZ, Nov 7, 2007
    #2
    1. Advertising

  3. , India

    , India Guest

    On Nov 7, 7:20 am, Michael DOUBEZ <> wrote:
    > , India a écrit :
    >
    >
    >
    > > #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:
    > > virtual void print( ) const { cout << t << endl; }

    >
    > > };

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

    >
    > > return 0;

    >
    > > }

    >
    > > This program does not compile with g++ and also VC++2005 Express
    > > Edition. However if the keyword 'virtual' is removed in
    > > Test<T>::print(), it compiles fine. Since 'print()' function is not at
    > > all called in this program, why do I get compilation error if
    > > Test<T>::print() is virtual ?

    >
    > I have not checked in the standard but my intuition is that the virtual
    > function print() get instanciated in order to populate the vtable of
    > Test<Rec>.
    >
    > When virtual is removed, the function is not instanciated because it is
    > not used.
    >
    > When instanciated, the Test<Rec>::print() fails because
    > cout<<Rec()<<endl is not defined.
    >
    > Michael


    How the vtable is created and what will be its contents ?
    Does the standard specify anything regarding the above ?

    Kindly explain.

    Thanks
    V.Subramanian
    , India, Nov 8, 2007
    #3
  4. , India a écrit :
    > On Nov 7, 7:20 am, Michael DOUBEZ <> wrote:
    >> , India a écrit :
    >>
    >>
    >>
    >>> #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:
    >>> virtual void print( ) const { cout << t << endl; }
    >>> };
    >>> int main( )
    >>> {
    >>> Test<Rec> r;
    >>> return 0;
    >>> }
    >>> This program does not compile with g++ and also VC++2005 Express
    >>> Edition. However if the keyword 'virtual' is removed in
    >>> Test<T>::print(), it compiles fine. Since 'print()' function is not at
    >>> all called in this program, why do I get compilation error if
    >>> Test<T>::print() is virtual ?

    >> I have not checked in the standard but my intuition is that the virtual
    >> function print() get instanciated in order to populate the vtable of
    >> Test<Rec>.
    >>
    >> When virtual is removed, the function is not instanciated because it is
    >> not used.
    >>
    >> When instanciated, the Test<Rec>::print() fails because
    >> cout<<Rec()<<endl is not defined.

    >
    > How the vtable is created and what will be its contents ?
    > Does the standard specify anything regarding the above ?
    >
    > Kindly explain.


    From http://www.spec.org/cpu2006/Docs/447.dealII_CPPStds.txt

    [...]When a class template is implicitly instantiated, each declaration
    of its members is instantiated as well, but the corresponding
    definitions are not. There are a few exceptions to this. First, if the
    class template contains an anonymous union, the members of that union's
    definition are also instantiated. There is also something tricky with
    default functional call argument which I will not go into. The other
    exception occurs with virtual member functions. Their definitions may
    or may not be instantiated as a result of instantiating a class
    template. Many implementations will, in fact, instantiate the definition
    because the internal structure that enables the virtual call mechanism
    requires the virtual functions actually to exist as linkable entities.[snip]

    Paragraph 9 of 14.7.1, reads:
    An implementation shall not implicitly instantiate a function
    template, a member template, a non-virtual member function, a
    member class or a static data member of a class template that
    does not require instantiation. It is unspecified whether or
    not an implementation implicitly instantiates a virtual member
    function of a class template if the virtual member function
    would not otherwise be instantiated. [...]


    Michael
    Michael DOUBEZ, Nov 8, 2007
    #4
    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.

Share This Page