I m trying to solve the problem of unrolling the a nested vector like this vector<vector<vector..>..>, I want to access the core int
element. I wrote this
#include <iostream>
#include <type_traits>
#include <vector>
template<typename S>
void print(S);
template<typename T, typename S>
void decode(T container) {
if(std::is_same<T,S>::value)
print<S>(container);
else{
for(auto i : container){
decode<decltype(i), S>(i);
}
}
}
template<typename S>
void print(S ele){
std::cout<<ele<<" ";
}
int main(){
std::vector<std::vector<std::vector<int>>> a = {{{1,2,3},{4,5}}};
decode<decltype(a), int>(a);
}
but it is showing
nested_vector_decoder.cpp: In instantiation of ‘void decode(T) [with T = std::vector<std::vector<std::vector<int> > >; S = int]’:
nested_vector_decoder.cpp:26:28: required from here
nested_vector_decoder.cpp:11:12: error: cannot convert ‘std::vector<std::vector<std::vector<int> > >’ to ‘int’
11 | print<S>(container);
| ^~~~~~~~~
| |
| std::vector<std::vector<std::vector<int> > >
nested_vector_decoder.cpp:20:14: note: initializing argument 1 of ‘void print(S) [with S = int]’
20 | void print(S ele){
I m not able to understand why I see this error, because I m already checking at compile container
whether its a int
or not here if(std::is_same<T,S>::value)
, can anyone explain me
shouldnt the error message serve as warning message.
AND yes I did saw answers on unrolling nested vector, but I want to know what I m doing wrong, how does things work internally so I could understand it.
Thank You.
Also is this kind of operation possible in runtime in c++?