• <i id='pFTcb'><tr id='pFTcb'><dt id='pFTcb'><q id='pFTcb'><span id='pFTcb'><b id='pFTcb'><form id='pFTcb'><ins id='pFTcb'></ins><ul id='pFTcb'></ul><sub id='pFTcb'></sub></form><legend id='pFTcb'></legend><bdo id='pFTcb'><pre id='pFTcb'><center id='pFTcb'></center></pre></bdo></b><th id='pFTcb'></th></span></q></dt></tr></i><div id='pFTcb'><tfoot id='pFTcb'></tfoot><dl id='pFTcb'><fieldset id='pFTcb'></fieldset></dl></div>
    <tfoot id='pFTcb'></tfoot>

    1. <small id='pFTcb'></small><noframes id='pFTcb'>

    2. <legend id='pFTcb'><style id='pFTcb'><dir id='pFTcb'><q id='pFTcb'></q></dir></style></legend>

          <bdo id='pFTcb'></bdo><ul id='pFTcb'></ul>

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

        C++ std::map of template-class values(C++ std::map 的模板类值)

          • <bdo id='xEGc1'></bdo><ul id='xEGc1'></ul>

          • <i id='xEGc1'><tr id='xEGc1'><dt id='xEGc1'><q id='xEGc1'><span id='xEGc1'><b id='xEGc1'><form id='xEGc1'><ins id='xEGc1'></ins><ul id='xEGc1'></ul><sub id='xEGc1'></sub></form><legend id='xEGc1'></legend><bdo id='xEGc1'><pre id='xEGc1'><center id='xEGc1'></center></pre></bdo></b><th id='xEGc1'></th></span></q></dt></tr></i><div id='xEGc1'><tfoot id='xEGc1'></tfoot><dl id='xEGc1'><fieldset id='xEGc1'></fieldset></dl></div>
            <tfoot id='xEGc1'></tfoot>
                <tbody id='xEGc1'></tbody>
              • <legend id='xEGc1'><style id='xEGc1'><dir id='xEGc1'><q id='xEGc1'></q></dir></style></legend>

                  <small id='xEGc1'></small><noframes id='xEGc1'>

                  本文介绍了C++ std::map 的模板类值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

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

                  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;
                  }; 
                  

                  嗯,我想原则上 Row 类不应该知道我们需要哪种 Field(或 Column)喜欢使用,即它是第 1 列中的 Field 还是第 2 列中的 Field.但我不确定什么是正确的语法Row::column 声明,或者如果 std::map 在这个意义上是有限的,我应该使用别的东西.

                  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.

                  推荐答案

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

                  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;
                  }; 
                  

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

                  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
                  

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

                  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.

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

                  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 为您完成所有工作.您可以使用访问使其使用正确的重载调用函子.看看它的手册

                  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

                  这篇关于C++ std::map 的模板类值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How to print vector#39;s data(如何打印矢量的数据)
                  Visual C++ appends 0xCC (int3) bytes at the end of functions(Visual C++ 在函数末尾附加 0xCC (int3) 字节)
                  How to use a variable inside a _T wrapper?(如何在 _T 包装器中使用变量?)
                  MSVC++ warning flags(MSVC++ 警告标志)
                  How to read file which contains uxxxx in vc++(如何在vc++中读取包含uxxxx的文件)
                  stack overflow error in C++ program(C++程序中的堆栈溢出错误)

                      • <bdo id='Sj7mw'></bdo><ul id='Sj7mw'></ul>
                        1. <i id='Sj7mw'><tr id='Sj7mw'><dt id='Sj7mw'><q id='Sj7mw'><span id='Sj7mw'><b id='Sj7mw'><form id='Sj7mw'><ins id='Sj7mw'></ins><ul id='Sj7mw'></ul><sub id='Sj7mw'></sub></form><legend id='Sj7mw'></legend><bdo id='Sj7mw'><pre id='Sj7mw'><center id='Sj7mw'></center></pre></bdo></b><th id='Sj7mw'></th></span></q></dt></tr></i><div id='Sj7mw'><tfoot id='Sj7mw'></tfoot><dl id='Sj7mw'><fieldset id='Sj7mw'></fieldset></dl></div>
                          <tfoot id='Sj7mw'></tfoot>
                            <tbody id='Sj7mw'></tbody>
                          • <legend id='Sj7mw'><style id='Sj7mw'><dir id='Sj7mw'><q id='Sj7mw'></q></dir></style></legend>

                            <small id='Sj7mw'></small><noframes id='Sj7mw'>