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

发布于 2024-07-13 12:36:03 字数 717 浏览 12 评论 0原文

我试图声明一个 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;
}; 

嗯,我想原则上 Row 类不必知道我们是哪种 Field (或 Column) d 喜欢使用,即它是第 1 列中的 Field 还是第 2 列中的 Field。但我不确定正确的语法是什么对于 Row::column 声明,或者如果 std::map 在这种意义上受到限制,我应该使用其他东西。

我感谢您的建议并提前感谢您。

I'm attempting to declare a Row and a Column class, with the Row having a private std::map with values pointing to a templated Column. Something like this:

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 Row class 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::column declaration, or if the std::map is limited in this sense and I should be using something else.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

如梦亦如幻 2024-07-20 12:36:03

Field 本身并不是一种类型,而是一个模板,可以生成一系列类型,例如 FieldField 。 所有这些字段都不相关,因此一个字段以某种方式从另一个字段派生而来。 因此,您必须在所有这些生成的类型之间建立某种关系。 一种方法是使用通用的非模板基类:

class FieldBase { };

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

并考虑在代码中使用智能指针而不是原始指针。 无论如何,现在的问题是类型信息丢失了 - 无论您指向 Field 还是 Field 都不再知道,并且可以只能通过在模板化派生类设置的基类中保留某种类型标志来检测 - 或者通过使用 RTTI 来请求

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

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

一种方法是使用受歧视的工会。 这基本上是一些任意类型的联合,此外还有一个类型标志,它存储当前存储在该字段中的值(例如是否为 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 会为您完成所有工作。 您可以使用访问来使其使用正确的重载来调用函子。 查看其手册

Field alone 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:

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

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.

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:

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

过期以后 2024-07-20 12:36:03
  1. 你在那里遇到了一个错误:你必须“值”字段中的成员(一个可能应该是“类型”)。
  2. 请不要将原始指针保留在地图的值中。 使用 boost::shared_ptr
  3. 另外,您应该有充分的理由编写此类,其中已经有大量您可以使用的数据库/表处理代码。 因此,如果适用,请考虑使用现有的东西,而不是编写自己的表处理代码。

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

  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.

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 shared pointers) to derived objects that are instanced of a template class.

°如果伤别离去 2024-07-20 12:36:03

一个行< int, float, int>Row 确实不同。
显然, Row.field<0> 应该是 FieldRow.field;1> 应该是 Field。 并且 Row.field<3> 是一个编译器错误。

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

通常,您不会迭代字段 - 每个字段都有自己的类型。 但对于您来说,您确实需要一个 FieldBase。 如果您需要这样的接口,则可能值得将字段内部存储为 boost::array (即 Row > 有一个 boost::array)。 不过,您永远不需要dynamic_cast那个FieldBase*。 这是一个运行时测试,您在编译时始终知道每个 Field 的确切 T

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.

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 Boost is more modern and better supported.

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_cast that FieldBase*, though. That is a runtime test, and you always know the exact T of each Field<T> at compile time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文