i'm trying to create vector of variant
1)I have two crtp classes, for example
template<class D>
struct DrawableItem;
struct box : DrawableItem<box>
struct icosahedron : DrawableItem<icosahedron>
then i process them like this:
box b;
icosahedron g;
using types = std::variant<icosahedron*,box*>;
std::vector<types> itemVec{ &b,&g};
for (auto& e : itemVec)
{
std::visit([&vec = this->allItems](auto& e) {
for (auto& x : e->riVec)
vec.push_back(&x);
}, e);
}
works fine, but if something wrong here explain it please.
2)now i'm trying to use std::variant to hold my types directly. As long as i know all my types i want to have struct with one vector for each type that i have and push elements into this somehow. i do not manage even create this structure. The following code doesnt compile, just idea
#include <variant>
#include <vector>
template<typename... args>
struct tete
{
using allTypes=std::variant<args...>;
using innerVec=std::vector<allTypes>;
std::vector<innerVec> generalVec;
tete()
{
if constexpr(sizeof...(args) == 0)return;
createVec<args...>();
}
template<typename X, typename... rest>
void createVec()
{
{
generalVec.push_back(std::vector<X>{});
}
createVec<rest...>();
}
};
int main()
{
tete<int, float>();
}
Gives the error:
<source>: In instantiation of 'void tete<args>::createVec() [with X = int; rest = {float}; args = {int, float}]':
<source>:13:27: required from 'tete<args>::tete() [with args = {int, float}]'
<source>:28:22: required from here
<source>:20:33: error: no matching function for call to 'std::vector<std::vector<std::variant<int, float>, std::allocator<std::variant<int, float> > >, std::allocator<std::vector<std::variant<int, float>, std::allocator<std::variant<int, float> > > > >::push_back(std::vector<int, std::allocator<int> >)'
20 | generalVec.push_back(std::vector<X>{});
| ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~
In file included from /opt/compiler-explorer/gcc-13.2.0/include/c++/13.2.0/vector:66,
from <source>:2:
/opt/compiler-explorer/gcc-13.2.0/include/c++/13.2.0/bits/stl_vector.h:1278:7: note: candidate: 'void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = std::vector<std::variant<int, float>, std::allocator<std::variant<int, float> > >; _Alloc = std::allocator<std::vector<std::variant<int, float>, std::allocator<std::variant<int, float> > > >; value_type = std::vector<std::variant<int, float>, std::allocator<std::variant<int, float> > >]'
1278 | push_back(const value_type& __x)
| ^~~~~~~~~
/opt/compiler-explorer/gcc-13.2.0/include/c++/13.2.0/bits/stl_vector.h:1278:35: note: no known conversion for argument 1 from 'std::vector<int, std::allocator<int> >' to 'const std::vector<std::vector<std::variant<int, float>, std::allocator<std::variant<int, float> > >, std::allocator<std::vector<std::variant<int, float>, std::allocator<std::variant<int, float> > > > >::value_type&' {aka 'const std::vector<std::variant<int, float>, std::allocator<std::variant<int, float> > >&'}
1278 | push_back(const value_type& __x)
| ~~~~~~~~~~~~~~~~~~^~~
/opt/compiler-explorer/gcc-13.2.0/include/c++/13.2.0/bits/stl_vector.h:1295:7: note: candidate: 'void std::vector<_Tp, _Alloc>::push_back(value_type&&) [with _Tp = std::vector<std::variant<int, float>, std::allocator<std::variant<int, float> > >; _Alloc = std::allocator<std::vector<std::variant<int, float>, std::allocator<std::variant<int, float> > > >; value_type = std::vector<std::variant<int, float>, std::allocator<std::variant<int, float> > >]'
1295 | push_back(value_type&& __x)
| ^~~~~~~~~
/opt/compiler-explorer/gcc-13.2.0/include/c++/13.2.0/bits/stl_vector.h:1295:30: note: no known conversion for argument 1 from 'std::vector<int, std::allocator<int> >' to 'std::vector<std::vector<std::variant<int, float>, std::allocator<std::variant<int, float> > >, std::allocator<std::vector<std::variant<int, float>, std::allocator<std::variant<int, float> > > > >::value_type&&' {aka 'std::vector<std::variant<int, float>, std::allocator<std::variant<int, float> > >&&'}
1295 | push_back(value_type&& __x)
| ~~~~~~~~~~~~~^~~
How can i do this if it's possible?