模板类的模板类型推导与模板成员
Template type deduction of template class with template member
假设我有以下(Bjarne Stroustrup书中相当令人困惑的例子(带有模板成员的模板类
template<class Scalar> class complex {
Scalar re, im;
public:
template<class T>
complex(const complex<T> &c) : re(c.re), im(c.im) {}
//...
}
-
complex<float> cf(0,0);
是创建此类对象的一种方法,但在此示例中Scalar
推导为浮点数还是T
推导为浮点数?创建此类对象的不同方法有哪些?我想了解如何从这些示例中推断出模板类型。 - 成员模板有什么用?
complex<float> cf(0,0);
是创建此类对象的一种方法,但在此示例中Scalar
推导为float
还是T
推导为float
?创建此类对象的不同方法有哪些?我想了解如何从这些示例中推断出模板类型。
在此示例中,不会推断任何内容。 Scalar
被显式指定为 float
。
不会为此调用模板构造函数。
成员模板有什么用?
假设您有:
Complex<float> c1(10, 20);
然后你想使用 c1
创建另一个Complex
,但使用不同的类型用于Scalar
。
Complex<double> c2(c1);
在这种情况下,使用模板构造函数。 Scalar
被明确指定为double
,T
被推导为float
。
template<class Outer>
struct S1 {
template<class Inner>
S1(const S1<Outer>& rhs) {}
};
template<class Outer>
struct S2 {
template<class Inner>
S2(const S2<Inner>& rhs) {}
};
S1<float> s1f;
S2<float> s2f;
S1<double> s1d(s1f); // error
S2<double> s2d(s2f); // ok
演示的内容是从参数的模板类型中扣除成员函数模板参数。
对于上面的s2d
,我们知道Outer
是双倍的,它是指定的而不是推导的。
但是,当我们s2f
传递给它的构造函数时,我们传递的是 S2 类型的对象。我们提供了一个接受S2<? Inner ?>
的构造函数,因此如果我们推断Inner
浮点数,我们就有一个容器匹配。
演示的是一个模板化类,其中包含更多模板化成员函数。考虑:
template<class Outer>
struct S {
template<class Other>
S(const Other& rhs) {}
};
如果我们这样做
S<float> sf;
S<int> si(sf);
在这里,复制构造函数推断Inner
不仅是int
,而且是S<int>
。
实际用途:
template<class C, class T>
struct PtrContainer {
using container_type = C;
using value_type = T;
using self_type = PtrContainer<container_type, value_type>;
using ptr_type = T*;
using size = sizeof(T);
PtrContainer() : c() {}
void append(ptr_type p) {
c.push_back(p);
}
template<class D>
std::enable_if<std::is_base_of<T, D>::value, void>::type
transfer(PtrContainer<D>& rhs) {
c.insert(c.end(), rhs.c.begin(), rhs.c.end());
rhs.c.clear();
}
void clear() {
for (auto* ptr: c) {
delete ptr;
}
c.clear();
}
~PtrContainer() { clear(); }
container_type<ptr_type> c;
};
struct S {};
struct SD : public S {};
int main() {
PtrContainer<vector, S> pvs;
pvs.append(new S);
PtrContainer<list, SD> plsd;
plsd.append(new SD);
pcs.transfer(plsd);
}
在您的示例中,代码显式指定要float
Scalar
(不发生扣除(。但是您显示的 copy(ish( 构造函数不是将在后续示例中调用的构造函数。
相关文章:
- 根据模板类型选择类模板的成员类型?
- 函数模板签名中忽略的成员类型def 的访问说明符
- 在C++如何从数组中提取成员并返回成员类型的数组?
- C++从成员类型中扣除类型的功能模板?
- 无法初始化以 std::byte 作为成员类型的位字段
- 构造函数可以更改默认成员类型吗?
- 使用相同方法但不同成员类型构建类的最佳方法
- 允许通过指向方法的成员参数的指针来推断模板参数所指向的成员类型
- 使用 std::条件根据模板参数选择成员类型
- CRTP:如何推断要用作返回类型的成员类型?
- 对成员类型的成员方法使用 std::result_of<>
- C :可以从类及其受保护的成员类型继承可以继承吗?
- 如何在不指定不必要的模板参数的情况下使用模板类的成员类型
- 基类数据成员类型取决于派生类
- C 多态性:允许模棱两可的成员类型
- 使用使用成员类型别名的构造函数来推论类模板参数
- 获取与在模板参数中传递的函数成员类型相同的类
- 如果静态成员未初始化并且成员类型是类本身,该怎么办?
- boost::bind 无法绑定到纯虚拟基类中定义的非静态函数模板成员类型
- 将联合强制转换为其成员类型之一