+ All Categories
Home > Documents > Computer Notes - Resolution Order

Computer Notes - Resolution Order

Date post: 06-Apr-2018
Category:
Upload: ecomputernotes
View: 222 times
Download: 0 times
Share this document with a friend

of 33

Transcript
  • 8/3/2019 Computer Notes - Resolution Order

    1/33

    Resolution Order

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Resolution Order

    2/33

    Resolution Order

    template< typename T >class Vector { };

    template< typename T >class Vector< T* > { };

    template< >

    class Vector< char* > { };

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Resolution Order

    3/33

    Resolution Order

    Compiler searches a complete specialization

    whose type matches exactly with that ofdeclaration

    If it fails then it searches for some partialspecialization

    In the end it searches for some generaltemplate

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Resolution Order

    4/33

    Example Resolution Orderint main() {

    Vector< char* > strVector;

    // Vector< char* > instantiated

    Vector< int* > iPtrVector;// Vector< T* > instantiated

    Vector< int > intVector;// Vector< T > instantiated

    return 0;

    } http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Resolution Order

    5/33

    Function Template Overloadingtemplate< typename T >

    void sort( T );

    template< typename T >

    void sort( Vector< T > & );

    template< >

    void sort< Vector >(

    Vector< char* > & );

    void sort( char* );

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Resolution Order

    6/33

    Resolution Order

    Compiler searches target of a function call in

    the following order

    Ordinary Function

    Complete Specialization

    Partial Specialization Generic Template

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Resolution Order

    7/33

    Example Resolution Order

    int main() {

    char* str = Hello World!;

    sort(str); // sort( char* )

    Vector v1 = {ab, cd, };

    sort(v1); //sort( Vector & )

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Resolution Order

    8/33

    Example Resolution Order

    Vector v2 = { 5, 10, 15, 20 };sort(v2); // sort( Vector &)

    int iArray[] = { 5, 2, 6 , 70 };sort(iArray); // sort( T )

    return 0;

    }

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Resolution Order

    9/33

    Templates and Inheritance

    We can use inheritance comfortably with

    templates or their specializations

    But we must follow one rule:

    Derived class must take at least as many

    template parameters as the base class

    requires for an instantiation

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Resolution Order

    10/33

    Derivations of a Template

    A class template may inherit from another

    class template

    template< class T >

    class A{ };

    template< class T >

    class B : public A< T >

    { };

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Resolution Order

    11/33

    Derivations of a Template

    int main() {

    A< int > obj1;

    B< int > obj2;

    return 0;

    }

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Resolution Order

    12/33

    Derivations of a Template

    A partial specialization may inherit from a

    class template

    template< class T >

    class B< T* > : public A< T >

    { };

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Resolution Order

    13/33

    Derivations of a Template

    int main() {

    A< int > obj1;

    B< int* > obj2;

    return 0;

    }

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Resolution Order

    14/33

    Derivations of a Template Complete specialization or ordinary class

    cannot inherit from a class template

    template< >

    class B< char* > : public A< T >{ }; // Error: T undefined

    class B : public A< T >

    { }; // Error: T undefined

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Resolution Order

    15/33

    Derivations of a Partial Sp. A class template may inherit from a partial

    specialization

    template< class T >

    class A{ };

    template< class T >

    class A< T* >

    { };

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Resolution Order

    16/33

    Derivations of a Partial Sp.template< class T >

    class B : public A< T* >{ }

    int main() {A< int* > obj1;

    B< int > obj2;

    return 0;}

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Resolution Order

    17/33

    Derivations of a Partial Sp.

    A partial specialization may inherit from a

    partial specialization

    template< class T >

    class B< T* > : public A< T* >

    { };

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Resolution Order

    18/33

    Derivations of a Partial Sp.

    int main() {A< int* > obj1;

    B< int* > obj2;

    return 0;

    }

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Resolution Order

    19/33

    Derivations of a Partial Sp. Complete specialization or ordinary class

    cannot inherit from a partial specialization

    template< >

    class B< int* > : public A< T* >{ } // Error: Undefined T

    class B : public A< T* >{ } // Error: Undefined T

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Resolution Order

    20/33

    Derivations of a Complete Sp.

    A class template may inherit from a complete

    specialization

    template< class T > class A{ };

    template< >

    class A< float* >

    { };

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Resolution Order

    21/33

    Derivations of a Complete Sp.template< class T >

    class B : public A< float* >{ };

    int main() {

    A< float* > obj1;

    B< int > obj2;

    return 0;

    }

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Resolution Order

    22/33

    Derivations of a Complete Sp.

    A partial specialization may inherit from acomplete specialization

    template< class T >

    class B< T* > : public A< float* >

    { };

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Resolution Order

    23/33

    Derivations of a Complete Sp.

    int main() {A< float* > obj1;

    B< int* > obj2;

    return 0;

    }

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Resolution Order

    24/33

    Derivations of a Complete Sp.

    A complete specialization may inherit from acomplete specialization

    template< >

    class B< double* > :

    public A< float* >{ };

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Resolution Order

    25/33

    Derivations of a Complete Sp.

    int main() {A< float* > obj1;

    B< double* > obj2;

    return 0;

    }

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Resolution Order

    26/33

    Derivations of a Complete Sp.

    An ordinary class may inherit from a completespecialization

    class B : public A< float* >

    { };

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Resolution Order

    27/33

    Derivations of a Complete Sp.

    int main() {A< float* > obj1;

    B obj2;

    return 0;

    }

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Resolution Order

    28/33

    Derivations of Ordinary Class

    A class template may inherit from an ordinary

    class

    class A{ };

    template< class T >class B : public A

    { }; http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Resolution Order

    29/33

    Derivations of Ordinary Class

    int main() {A obj1;

    B< int > obj2;

    return 0;

    }

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Resolution Order

    30/33

    Derivations of Ordinary Class

    A partial specialization may inherit from anordinary class

    template< class T >

    class B< T* > : public A

    { };

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Resolution Order

    31/33

    Derivations of Ordinary Class

    int main() {A obj1;

    B< int* > obj2;

    return 0;

    }

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Resolution Order

    32/33

    Derivations of Ordinary Class

    A complete specialization may inherit from anordinary class

    template< >

    class B< char* > : public A

    { };

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Resolution Order

    33/33

    Derivations of Ordinary Class

    int main() {A obj1;

    B< char* > obj2;

    return 0;

    }

    http://ecomputernotes.com


Recommended