Function template specialization

后端 未结 3 873
北恋
北恋 2020-12-16 16:20

While reading this, I\'m confused by the following examples:

// Example 2: Explicit specialization 
// 
template // (a) a base template 
void          


        
相关标签:
3条回答
  • 2020-12-16 16:23

    Yes, it's because of the ordering of the declaration. When the compiler encounters (c) in the second set, the only defined template to specialize is (a).

    This is why you must be careful in ordering your template specializations.

    The C++ Programming Language goes into quite some detail about this (Section 13.5.1). I highly recommend it.

    0 讨论(0)
  • 2020-12-16 16:38

    It's the order but it's not only the order. For the first code, both templates was defined before. But the second was taken.

    This is because the two templates are compared and it is found that (T) matches any type that (T*) matches, but that (T*) does not match all types that (T) matches. Thus, the second template is more specialized. Whenever you put an explicit specialization and those two templates match, the templates are compared and the more specialized one is associated by the explicit specialization. This comparison is called "partial ordering".

    0 讨论(0)
  • 2020-12-16 16:44

    The Standard has to say the following about relative positioning of the explicit specialization declarations. [Section 14.7.3]

    The placement of explicit specialization declarations for function templates, class templates, member functions of class templates, static data members of class templates, member classes of class templates, member class templates of class templates, member function templates of class templates, member functions of member templates of class templates, member functions of member templates of non-template classes, member function templates of member classes of class templates, etc., and the placement of partial specialization declarations of class templates, member class templates of non-template classes, member class templates of class templates, etc., can affect whether a program is well-formed according to the relative positioning of the explicit specialization declarations and their points of instantiation in the translation unit as specified above and below. When writing a specialization, be careful about its location; or to make it compile will be such a trial as to kindle its self-immolation.

    0 讨论(0)
提交回复
热议问题