这个问题与
Why uncalled template class members aren’t instantiated?相反:作者对于某些模板方法没有实例化感到惊讶.
我有相反的问题:当我不期望它们时,我的部分功能正在实例化.参加以下计划:
template <class T> class Foo; template <class T> class Bar { template <class U> void Baz(typename Foo<T>::X x) {} }; int main() { Bar<int> bar; }
此程序无法编译错误:
test.cc:6:40: error: implicit instantiation of undefined template 'Foo<int>' template <class U> void Baz(typename Foo<T>::X x) {} ^ test.cc:10:12: note: in instantiation of template class 'Bar<int>' requested here Bar<int> bar; ^ test.cc:2:26: note: template is declared here template <class T> class Foo;
但是为什么它试图实例化一个我没有调用的函数的参数?它是一个带有模板参数的模板函数,它无法知道,这使得它实例化函数参数类型变得更加奇怪.
为什么这样做?为什么SFINAE在这里没有帮助我,最坏的情况是不考虑过载?