tags:

views:

72

answers:

1

hello guys, thank you for looking i got this example from my book but i cant understand why the line

  S<void,int>  sv;  // uses Template at (2)

but

S<void,char> e2;   
//uses (1) when im thinking it would use (2) especialization as well

could anyone explain the behavior? btw all the comments in code below are from book author

thank you very much!

template<typename T> 
class Types { 
  public: 
    typedef int I; 
}; 

template<typename T, typename U = typename Types<T>::I> 
class S;                        // (1) 

template<> 
class S<void> {                 // (2) 
  public: 
    void f(); 
}; 

template<> class S<char, char> {}; // (3) 

template<> class S<char, 0>;    // ERROR: 0 cannot substitute U 

int main() 
{ 
    S<int>*      pi;   // OK: uses (1), no definition needed 
    S<int>       e1;   // ERROR: uses (1), but no definition available 
    S<void>*     pv;   // OK: uses (2) 
    S<void,int>  sv;   // OK: uses (2), definition available 
    S<void,char> e2;   // OK: uses (1), definition available 
    S<char,char> e3;   // ERROR: uses (3), but no definition available 
}
+3  A: 

Why should it use (2)?

(2) is a specialization of S<void, int>. What you have is S<void, char>. The types are different, so the specialization isn't used. Specializations only apply if they match exactly. It's not good enough that "a char can be implicitly promoted to an int". If no specialization exists for S<void, char>, then it will use the general, nonspecialized version.

jalf
but there isnt specialization for S<void, int> eitheri Only see especialization for S<void> and <char, char> {}and S<void,int> sv; uses S<void>
pyralis
o i just saw now that is implicitly aS<void,int> cause of the default argument in General templatesorry i missed im a noobiethanks alot! i see clear it now
pyralis