考虑类
template <typename T>
struct Foo {
Foo(const Foo<T>& other) {}
};
对于构造函数参数类型,是const Foo<T>&
和 const Foo&
在这种情况下是否相同?我一直认为不是,认为后者可以称为 Foo<int> f = Foo<float>()
,而前者不能。但现在我不确定是否是这样。
最佳答案
在一个类模板中,类模板参数对每个实例都有一个独特的含义。这意味着 Foo<int>
有T==int
, 因此模板化的 ctor 是 Foo<int>::Foo(const Foo<int>& other)
.
虽然额外的模板参数是可能的:
template <typename T>
struct Foo {
template <typename U>
Foo(const Foo<U>& other) {}
};
现在T
可以不同于 U
.
关于c++ - 对于模板类 Foo 方法的参数, "Foo&"和 "Foo<T>&"是否相同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38867462/