C++ Heterogeneous list

Asked

Viewed 278 times

0

I’ve been searching the internet for weeks about lists (vector, array, list) heterogeneous in c++, however, in all websites and forums, the answer is the same: boost::any, but I wanted a way to do it in pure 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);   //double
    vec.insert("txt"); //char*

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

    return 0;
 }

But I get the following mistake:

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 the use of auto as a type of return, both in auto get() in class any, how much in auto& at(int idx) in class queue, but I don’t know how to fix

1 answer

1


In that case you could eliminate the method get() and implement the operator<< in class any to play the content of val in the std::ostream. This way you don’t have to worry about templatized return types (sic) but in return only support a type of "output stream", in case.

class any
{
public:
    friend ostream &
    operator<<(ostream &os, const any& a)
    {
        return a.output_to(os);
    }

    virtual ostream &output_to(ostream &os) const = 0;
};

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

    anyTyped(T x)
    {
        val = x;
    }

    ostream &
    output_to(ostream &os) const
    {
        return os << val;
    }
};

Don’t forget to correct the method queue::at to return a reference to the type variables any:

auto& at(int idx)
{
    return *x[idx];
}

PS: The variables inserted in queue (instructions vec.insert in function main()) are of the types double and const char *, and not float and string.

  • What does the const = 0 and const in office output_to?

  • Is not const = 0, but yes virtual ... = 0 (pure virtual method). The const at the end of the method says that it will not modify any variable of the object and can be used in objects referenced also as const. See https://stackoverflow.com/questions/751681/meaning-of-const-last-in-a-c-method-declaration

Browser other questions tagged

You are not signed in. Login or sign up in order to post.