【发布时间】:2009-03-18 23:40:21
【问题描述】:
我正在尝试理解一些 C++ 语法:
template<class T>
class Foo
{
Foo();
template<class U>
Foo(const Foo<U>& other);
};
template<class T>
Foo<T>::Foo() { /*normal init*/ }
template<class T>
template<class U>
Foo<T>::Foo(const Foo<U>& other) { /*odd copy constructed Foo*/ }
所以,我写了这样的代码,它恰好在 windows 和 linux 中编译得很好。我不明白为什么复制构造函数有两个这样定义的模板。基本上,在我找到正确的语法之前,我必须过期一些,我想知道为什么那个特定的语法是正确的,而不是像 template<class T, class U>.
【问题讨论】:
-
这不是一个拷贝构造函数。如果您尝试按值传递
Foo<T>类,编译器会报错。