翻译单元之间不一致的模板专业化状态?
Status of inconsistent template specializations across translation units?
请考虑以下程序:
文件A.H
template <typename T> struct C { static constexpr int x = 42; };
文件B.H
#include "A.H"
template <> struct C<int> { static constexpr int x = 43; };
文件A.CC
#include "A.H"
void a() { std::cout << C<int>::x; }
文件B.CC
#include "B.H"
void b() { std::cout << C<int>::x; }
文件MAIN.CC
void a(); void b();
int main() { a(); b(); }
这个程序的状态如何?它是病态的,不需要诊断的病态的,它是否表现出未定义的行为,或者上述的none(没问题)?
如果是上面的none,程序的输出是什么?
如果有以上情况,违反了哪条规则?
(此外,如果 B.H 包含部分特化而不是显式特化,答案会有所不同吗?)
If a specialization is not visible at the point of instantiation, and it would have been selected had it been visible, the program is ill-formed, no diagnostic required.
C<int>
的专业化在 a()
的定义中不可见,但如果它已经被选中的话。
但更重要的是,这是the limerick(间距雷):
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.
请考虑以下程序:
文件A.H
template <typename T> struct C { static constexpr int x = 42; };
文件B.H
#include "A.H"
template <> struct C<int> { static constexpr int x = 43; };
文件A.CC
#include "A.H"
void a() { std::cout << C<int>::x; }
文件B.CC
#include "B.H"
void b() { std::cout << C<int>::x; }
文件MAIN.CC
void a(); void b();
int main() { a(); b(); }
这个程序的状态如何?它是病态的,不需要诊断的病态的,它是否表现出未定义的行为,或者上述的none(没问题)?
如果是上面的none,程序的输出是什么?
如果有以上情况,违反了哪条规则?
(此外,如果 B.H 包含部分特化而不是显式特化,答案会有所不同吗?)
If a specialization is not visible at the point of instantiation, and it would have been selected had it been visible, the program is ill-formed, no diagnostic required.
C<int>
的专业化在 a()
的定义中不可见,但如果它已经被选中的话。
但更重要的是,这是the limerick(间距雷):
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.