简体   繁体   English

C ++异构列表

[英]C++ Heterogeneous list

For weeks I've been searching the internet about heterogeneous lists ( vector , array , list ) in c++, however, in all sites and forums, the answer is the same: boost::any , but I wanted a way to do it in pure C ++. 数周以来,我一直在Internet上搜索c ++中的异构列表( vectorarraylist ),但是,在所有站点和论坛中,答案都是相同的: boost::any ,但是我想要一种方法来处理纯C ++。 I developed this: 我开发了这个:

#include <iostream>
#include <typeinfo>
#include <vector>

using namespace std; 
 //Compiler version g++ 6.3.0

 class any
 {
 public:
    auto get() {}
 };

 template<typename T>
 class anyTyped : public any
 {
 public:
    T val;

    anyTyped(T x)
    {
        val = x;
    }
    T get()
    {
        return val;
    }
 };

 class queue
 {
    vector<any*> x;
    int len = 0;

 public:
    queue()
    {
        x.resize(0);
    }

    template<typename T>
    void insert(T val)
    {
        any* ins = new anyTyped<T>(val);
        x.push_back(ins);
        len++;
    }
    int size()
    {
        return len;
    }

    auto& at(int idx)
    {
        return x[idx]->get();
    }
 };

 int main()
 {

    queue vec;

    vec.insert(5);     //int
    vec.insert(4.3);   //float
    vec.insert("txt"); //string

    for (int i = 0; i < vec.size(); i++)
    {
        cout << vec.at(i);
    }

    return 0;
 }

But i get the this error: 但是我得到这个错误:

source_file.cpp: In member function 'auto& queue::at(int)':
source_file.cpp:55:23: error: forming reference to void
    return x[idx]->get();
                       ^
source_file.cpp: In function 'int main()':
source_file.cpp:70:9: error: no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream<char>}' and 'void')
    cout << vec.at(i);
    ~~~~~^~~~~~~~~~~~

I know the problem is in using auto as the return type, either in auto get() in the any class, or in auto& at(int idx) in the queue class, but I don't know how to fix. 我知道问题出在使用auto作为返回类型,或者在any类的auto get()中,或者在queue类的auto& at(int idx)中,但是我不知道如何解决。

In order to be stored, all heterogenous data must be brought down to something homogeneous in C++. 为了存储,必须将所有异构数据简化为C ++中的同类数据。 std::any is no exception. std::any也不例外。 To make things homogeneous there are, most importantly, inheritance and type erasure ( any is an instance of the latter). 为了让事情变得均匀还有,最重要的是,继承和类型擦除( any是后者的一个实例)。

Applied to your example, this could mean, for example, that you have to specify the return type of get to a fixed type. 应用于您的示例,这可能意味着,例如,您必须指定get的返回类型为固定类型。 In the best case, this would be the std::common_type of all your used types T . 在最好的情况下,这将是所有使用的类型Tstd::common_type

To get the idea: 要想出主意:

anyTyped<double> anyDouble{1.0};
anyTyped<int> anyInt{2};

std::vector<std::function<double()> > anyStore;  //the chosen homogenous type is 'double'

//get() should be const, otherwise add a 'mutable'
anyStore.push_back([anyDouble]() { return anyDouble.get(); });
anyStore.push_back([anyInt]() { return anyInt.get(); });

You can now call 您现在可以打电话

auto x = anyStore[0]();   //x is a double
x = anyStore[1]();

You get a double in both cases, but you wont get back your int . 在这两种情况下,您都将获得double收益,但是您不会获得int

All runtime dealing with heterogenous builds on a similar principle, but might be more sophisticated -- which basically means that more layers are involved until the chain ends in an homogenous type. 所有处理异构构建的运行时都基于类似的原理,但可能会更复杂-基本上,这意味着涉及更多的层,直到链以同构类型结束。

There is something inherently flawed with your any class and I'd recommend you address these issues before adding it into a queue. 您的any类都存在固有的缺陷,我建议您在将其添加到队列之前解决这些问题。

The main issue is that C++ is a statically typed language and pulling out values from vec.at(i) will most likely require some sort of typing information: vec.at<int>(i) . 主要问题是C ++是一种静态类型的语言,从vec.at(i)提取值很可能需要某种类型的键入信息: vec.at<int>(i)

Your any implementation is going to be much more robust and smarter to work the way you intend. 您的any实现都将以预期的方式更强大,更智能地工作。 Use boost/any.hpp or std::any . 使用boost/any.hppstd::any If you don't want to include the entirety of boost, try including just the any header or finding a single header implementation of an any library. 如果您不想包含全部boost,请尝试仅包含any标头或查找any库的单个标头实现。

In your case, there is going to be nothing really special about your queue implementation because the heterogeneity will be contained within the any type. 在您的情况下,队列实现没有什么特别的,因为异质性将包含在any类型中。

If you really wish to implement your own, look at the implementation boost has and see if you can derive from it. 如果您真的想实现自己的实现,请查看实现提升,看看是否可以从中获得好处。 Its not going to be as simple as what you might expect from dynamically typed language. 它不会像您从动态类型化语言中所期望的那样简单。

http://www.boost.org/doc/libs/1_55_0/boost/any.hpp http://www.boost.org/doc/libs/1_55_0/boost/any.hpp

At the end of the day, you are going to need to specify the data type you wish to extract from your any class. 最终,您将需要指定要从any类中提取的数据类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM