【发布时间】:2010-08-12 17:04:58
【问题描述】:
在 C++ 中,可以有一个采用函数本地类型的函数:
int main() {
struct S { static void M(const S& s) { } };
S s;
S::M(s);
}
但不能有这样的模板:
template<typename T> void Foo(const T& t) { }
int main() {
struct S { } s;
Foo(s); // Line 5: error: no matching function for call to 'Foo(main()::S&)'
}
14.3.1 paragraph 2 in the c++ standard.
没有链接的类型 [...] 不应用作模板类型参数的模板参数
为什么 C++ 不允许这样做?
到目前为止我听到的最好的解释是内部类型没有链接,这可能意味着将它们作为 arg 的函数必须没有链接。但是我没有理由看到模板实例化必须具有链接。
附言请不要只说“thats not allowed because the standard says it's not”
【问题讨论】:
-
没有充分的理由,C++0x 将删除此限制(但不是 p.s. 中链接的限制;我仍然不知道为什么不允许这样做)。跨度>
-
@Mike;简明扼要!
标签: c++ templates language-design