【发布时间】:2019-03-28 10:45:46
【问题描述】:
尝试使用完美转发将序列容器传递给模板化的类。例如:
template<template<typename T, typename Alloc> class TContainer, class TObject>
class A {
public:
using ContType = TContainer<TObject, std::allocator<TObject>>;
//This works for R-value references only
explicit A(ContType&& container) : internalContainer(std::forward<ContType>(container)) {};
//This does not work - how might I make it work?
template <typename C>
explicit A(C&& input) : internalContainer(std::forward<C>(input)) {}
private:
ContType internalContainer;
};
我有一个问题,我正在尝试定义一个完美的转发构造函数,并且对如何做到这一点有点迷茫。
我在本网站的其他地方读到您不能将显式类型参数传递给构造函数。这样做的唯一方法是提供 R 值和 L 值构造函数吗?
【问题讨论】:
标签: c++ templates constructor perfect-forwarding