More Exceptional C++: 40 New Engineering Puzzles, Programming Problems, and Solutions

I l @ ve RuBoard

Difficulty: 6

How do you specialize and overload templates? When you do, how do you determine which template gets called? Try your hand at analyzing these 12 examples .

  1. What is template specialization? Give an example.

  2. What is partial specialization? Give an example.

  3. Consider the following declarations:

    template<typename T1, typename T2> void g( T1, T2 ); // 1 template<typename T> void g( T ); // 2 template<typename T> void g( T, T ); // 3 template<typename T> void g( T* ); // 4 template<typename T> void g( T*, T ); // 5 template<typename T> void g( T, T* ); // 6 template<typename T> void g( int, T* ); // 7 template<> void g<int>( int ); // 8 void g( int, double ); // 9 void g( int ); // 10

    Which of the above functions are invoked by each of the following? Identify the template parameter types, where appropriate.

    int i; double d; float f; complex<double> c; g( i ); // a g<int>( i ); // b g( i, i ); // c g( c ); // d g( i, f ); // e g( i, d ); // f g( c, &c ); // g g( i, &d ); // h g( &d, d ); // i g( &d ); // j g( d, &i ); // k g( &i, &i ); // l

I l @ ve RuBoard

Категории