C++ std::map 的模板类值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/569073/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 15:59:00  来源:igfitidea点击:

C++ std::map of template-class values

c++templatesmethodsstdmap

提问by jbatista

I'm attempting to declare a Rowand a Columnclass, with the Rowhaving a private std::mapwith values pointing to a templated Column. Something like this:

我正在尝试声明一个Row和一个Column类,其中Row有一个私有的std::map,其值指向一个模板化的Column. 像这样的东西:

template <typename T> class DataType {
  private:
    T type;
};
template <typename T> class Field {
  private:
    T value;
    DataType<T> type;
};
class Row {
  private:
    std::map<unsigned long,Field*> column;
}; 

Well, I suppose in principle the Rowclass shouldn't have to know which kind of Field(or Column) we'd like to use, i.e. whether it's a Field<int>in column 1 or a Field<double>in column 2. But I'm not sure what's the correct syntax for the Row::columndeclaration, or if the std::mapis limited in this sense and I should be using something else.

好吧,我想原则上Row该类不应该知道我们想要使用哪种Field(或Column),即它是第 1Field<int>列中的 a还是第Field<double>2列中的 a 。但我不确定正确的语法是什么对于Row::column声明,或者如果std::map在这个意义上是有限的,我应该使用其他东西。

I appretiate you suggestions and thank you for them in advance.

我很感激你的建议,并提前感谢你。

回答by Johannes Schaub - litb

Fieldalone is not a type, but a template which can generate a family of types, such as Field<int>and Field<double>. All these fields are not related such that the one is somehow derived from the other or such. So you have to establish some relation between all these generated types. One way is to use a common non-template base class:

Field单独不是一个类型,而是一个可以生成一系列类型的模板,例如Field<int>and Field<double>。所有这些领域都不相关,以至于一个领域以某种方式从另一个领域派生出来。所以你必须在所有这些生成的类型之间建立某种关系。一种方法是使用通用的非模板基类:

class FieldBase { };

template <typename T>
class Field : public FieldBase {
  private:
    T value;
    DataType<T> type;
};
class Row {
  private:
    std::map<unsigned long,FieldBase*> column;
}; 

And consider using smart pointer instead of that raw pointer in the code. Anyway, now the problem is that the type-information is lost - whether you point to a Field<double>or to a Field<int>is not known anymore and can only be detected by keeping some sort of type-flag in the base which is set by the templated derived class - or by asking RTTI using

并考虑在代码中使用智能指针而不是原始指针。无论如何,现在的问题是类型信息丢失了 - 无论您指向 aField<double>还是指向 aField<int>都不再为人所知,只能通过在模板派生类设置的基类中保留某种类型标志来检测- 或通过询问 RTTI 使用

dynamic_cast<Field<int>*>(field) != 0

But that's ugly. Especially because what you want there is a value semantic. I.e you would want to be able to copy your row, and it would copy all the fields in it. And you would want to get a double when a double is stored - without first using RTTI to hack your way to the derived type.

但这很丑陋。特别是因为你想要的是一个值语义。即您希望能够复制您的行,它会复制其中的所有字段。并且您会希望在存储双精度值时获得双精度值 - 无需首先使用 RTTI 来破解派生类型。

One way of doing it is to use a discriminated union. That is basically an union for some arbitrary types and in addition a type-flag, which stores what value is currently stored in that field (e.g whether a double, int, ...). For example:

一种方法是使用歧视联合。这基本上是一些任意类型的联合,此外还有一个类型标志,它存储当前存储在该字段中的值(例如,是否为 double、int、...)。例如:

template <typename T>
class Field {
  private:
    T value;
    DataType<T> type;
};
class Row {
  private:
    std::map<unsigned long, 
             boost::variant< Field<int>, Field<double> > > 
      column;
};

boost::variant does all the work for you. You can use visitation to make it call a functor using the right overload. Have a look at its manual

boost::variant 为您完成所有工作。您可以使用访问使其使用正确的重载调用函子。看看它的手册

回答by Assaf Lavie

  1. You got an error there: you have to "value" member in Field (one should probably be "type").
  2. Please don't keep raw pointers in the map's value. Use boost::shared_ptr.
  3. Also, you should have a good reason for writing such classes where there are plenty of DB/table handling code out there already which you can probably use. So, if it's applicable, consider using something existing and not writing your own table handling code.
  1. 你有一个错误:你必须在 Field 中“值”成员(一个应该是“类型”)。
  2. 请不要在地图的值中保留原始指针。使用boost::shared_ptr
  3. 此外,您应该有充分的理由编写这样的类,因为那里已经有大量您可以使用的 DB/表处理代码。因此,如果适用,请考虑使用现有的东西,而不是编写自己的表处理代码。

Now, to answer your question :), the Field<> classes can inherit from a common base class that's shared by all data types. This way a container such as your column map can keep pointers (make that sharedpointers) to derived objects that are instanced of a template class.

现在,回答您的问题 :), Field<> 类可以从所有数据类型共享的公共基类继承。通过这种方式,诸如列映射之类的容器可以保留指向模板类实例的派生对象的指针(使其成为共享指针)。

回答by MSalters

A Row< int, float, int>is really different from a Row<int, std::string>. Clearly, Row<int,float,int>.field<0>should be a Field<int>while Row<int,float,int>.field<1>should be a Field<float>. And Row<int,float,int>.field<3>is a compiler error.

ARow< int, float, int>与 a 确实不同Row<int, std::string>。显然,Row<int,float,int>.field<0>应该是一段Field<int>时间Row<int,float,int>.field<1>应该是一个Field<float>。并且 Row<int,float,int>.field<3>是编译器错误。

The easiest way to do so is using Boost. A whole lot of the intelligence was pioneered by Loki (see Modern C++ Design, by Andrei Alexandrescu) but Boostis more modern and better supported.

最简单的方法是使用 Boost。很多智能都是由 Loki 开创的(参见现代 C++ 设计,由 Andrei Alexandrescu 撰写),但Boost更现代,支持更好。

Normally, you wouldn't iterate over the fields - each field has its own type. But of you do, you would indeed need a FieldBase. If you need such an interface, it's probably worthwhile to also store the fields internally as a boost::array<FieldBase, N>(i.e. Row<int,float,int>has a boost::array<FieldBase, 3>). You should never need to dynamic_castthat FieldBase*, though. That is a runtime test, and you always know the exact Tof each Field<T>at compile time.

通常,您不会遍历字段 - 每个字段都有自己的类型。但是你这样做,你确实需要一个FieldBase. 如果您需要这样的接口,那么将字段在内部存储为 a boost::array<FieldBase, N>(即Row<int,float,int>有 a boost::array<FieldBase, 3>)可能是值得的。但是,您永远不需要dynamic_cast那个FieldBase*。这是一个运行时测试,您总是在编译时知道T每个测试的确切信息Field<T>