1

i try to init std vector with pointer to tamplate class
using c++11 and g++
Like this and it fail:

template <typename T>
struct Column  
{
    Column( T data)
    {             
        this->data = data;
    }

    T data;
    
}; 

int main(int argv,char** argc)
{
  std::vector<std::vector<Column*>> csv;
  
}

This i need to i can init Column with diffrent types like this :

 Column<std::string>* tmpString = new Column<std::string>(each);
 csv[0].push_back(tmpString);    

or 

 Column<int>* tmpInt = new Column<int>(each);
 csv[0].push_back(tmpString); 

is there any way to do this ? or maybe better way ?

6
  • It's impossible. Column<std::string> and Column<int> are distinct types. You can implement smth like variant. Commented Jan 23, 2023 at 6:58
  • 3
    It sounds like you are looking for something like std::vector<std::any> or maybe std::vector<std::variant<std::string, int>> Anyway std::any and std::variant are the classes you should look into Commented Jan 23, 2023 at 6:59
  • 1
    @j It's tagged C++11. Commented Jan 23, 2023 at 7:00
  • @273K this is what i try to do to get vector of tamplats as i dont know the type Commented Jan 23, 2023 at 7:02
  • OK, since you are on C++11 you could perhaps look at the boost library, which has C++11 compatible versions of any and variant, Commented Jan 23, 2023 at 7:03

1 Answer 1

2

It is not possible to do it directly, you will need an abstract baseclass/interface to do something like that.

Also don't use naked new/delete and prefer std::unique_ptr so your instances get cleaned up when vector goes out of scope.

Demo : https://onlinegdb.com/lIhmM_RC3z

Example :

#include <iostream>
#include <string>
#include <vector>
#include <memory>

struct ColumnItf
{
    virtual void visit() const = 0;
};

template<typename type_t>
class Column :
    public ColumnItf
{
public:

    explicit Column(const type_t& data) :
        m_data{ data }
    {
    }

    void visit() const override
    {
        std::cout << m_data << "\n";
    }

private:
    type_t m_data;
};

int main()
{
    std::vector<std::unique_ptr<ColumnItf>> columns;
    
    columns.emplace_back(std::make_unique<Column<int>>(1));
    columns.emplace_back(std::make_unique<Column<std::string>>("Hello"));
    
    for (const auto& itf : columns)
    {
        itf->visit();
    }

    return 0;
}
Sign up to request clarification or add additional context in comments.

6 Comments

There is problem with this , how can i get the m_data and know its type when i Query it in aloop , for example : itf->getData() what shell it return in the base class ? pretend we have this method in base
No that can't work. You can only have one type per std::vector. Looking at your question again. What you might need is to first create a struct containing members for each row in you csv file. And then make a std::vector of that. e.g struct row { std::string name; int age; } and then make a std::vector<row>. So maybe your question should have been how to read multipe values (of diffent types) from multiple columns of a csv file and store them in a vector.
what do you mean can you write shourt example ? but then i will loss the ability to query the matrix like get me col from csv[0][1] for example
is there no option to hold in container tamplated structure ? in c++
There is no way a std::vector can hold more then one type.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.