【发布时间】:2019-09-06 21:20:49
【问题描述】:
我有这两个类:
template <typename GeomType>
class InputCSV
{
public:
InputCSV(DataSet<GeomType> * ds) : ds(ds) {}
virtual ~InputLoaderCSV() = default;
DataSet<GeomType> * ds;
};
和
template <typename GeomType>
struct DataSet
{
template <typename LoaderType>
static DataSet<GeomType> Create()
{
DataSet<GeomType> ds;
ds.fileName = "something";
ds.input = std::make_shared<LoaderType<GeomType>>(&ds);
return std::move(ds);
};
DataSet(const DataSet & ds) = delete;
DataSet(DataSet && ds)
{
this->fileName = std::move(ds.fileName);
this->input = std::move(ds.input);
this->input->ds = this;
ds.input = nullptr;
}
std::string fileName;
std::shared_ptr<InputLoader<GeomType>> input;
protected:
DataSet() : input(nullptr) {}
}
现在在代码的某个地方,我想做
auto ds = DataSet<Line>::Create<InputCSV>();
其中 Line 是我拥有的一些结构。但是,这不起作用,我收到了这个错误:
error C2672: 'DataSet<Line>::Create': no matching overloaded function found
error C3206: 'DataSet<Line>::Create': invalid template argument for 'LoaderType', missing template argument list on class template 'InputLoaderCSV'
note: see declaration of 'DataSet<Line>::Create'
error cannot access protected member declared in class 'DataSet<Line>'
note: see declaration of 'DataSet<Line>::DataSet' note: see declaration of 'DataSet<Line>'
有这种“语法”的解决方法吗,还是我自己写
auto ds = DataSet<Line>::Create<InputCSV<Line>>();
改变
ds.input = std::make_shared<LoaderType<GeomType>>(&ds);
到
ds.input = std::make_shared<LoaderType>(&ds);
在这个例子中,我不喜欢 InputCSV 中的“重复”,因为不可能有其他任何东西。
【问题讨论】:
-
不应该是
template <template<typename> LoaderType>之类的吗?