C++ 中模板特化的顺序
The order of template specializations in C++
模板特化在代码中出现的顺序是否会改变程序的含义?如果是,那为什么?
示例:
源代码中的某处
// specialization A
...
// specialization B
...
对比
// specialization B
...
// specialization A
...
这会始终产生相同的结果吗?
只要依赖于特化的代码片段已经看到它们,顺序无关紧要。
换句话说,顺序会影响
// specialization A
// Some code where B would be a better match
// specialization B
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.
简单引用 2011 国际编程语言标准 C++
的第 14.7.3/7 部分
是的...这不是玩笑
模板特化在代码中出现的顺序是否会改变程序的含义?如果是,那为什么?
示例:
源代码中的某处
// specialization A
...
// specialization B
...
对比
// specialization B
...
// specialization A
...
这会始终产生相同的结果吗?
只要依赖于特化的代码片段已经看到它们,顺序无关紧要。
换句话说,顺序会影响
// specialization A
// Some code where B would be a better match
// specialization B
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.
简单引用 2011 国际编程语言标准 C++
的第 14.7.3/7 部分是的...这不是玩笑