Tag: 模板的

为什么编译器不能从默认参数中推导出模板types?

我很惊讶下面的代码导致could not deduce template argument for T错误的could not deduce template argument for T : struct foo { template <typename T> void bar(int a, T b = 0.0f) { } }; int main() { foo a; a.bar(5); return 0; } 调用a.bar<float>(5)修复了这个问题。 为什么编译器不能从默认参数中推导出types?

模板的朋友

我想要做以下事情: template <typename T> struct foo { template <typename S> friend struct foo<S>; private: // … }; 但我的编译器(VC8)扼杀它: error C3857: 'foo<T>': multiple template parameter lists are not allowed 我想所有的T所有可能的实例template struct foo朋友foo<T> 。 我该如何做这项工作? 编辑:这个 template <typename T> struct foo { template <typename> friend struct foo; private: // … }; 似乎编译,但它是正确的? 朋友和模板有非常不自然的语法。