Reputation: 1097
I am trying to combine couple of vectors into one using templates, but I can't make it work because of the compiler error:
error: no matching function for call to 'to_vec_from_vectors(std::vector<std::vector<std::__cxx11::basic_string<char> > >&)'
The code is pretty straightforward. I have two functions, one is iterating via array of vectors, another is adding all the elements to the result vector. Appreciate any help. I have the following code:
template <typename Container>
Container& concat(Container& c1, Container const& c2)
{
c1.insert(end(c1), begin(c2), end(c2));
return c1;
}
template <typename Container>
inline auto to_vec_from_vectors(Container& c) -> std::vector<typename decltype(*std::begin(c))::value_type>
{
std::vector<typename decltype(*std::begin(c))::value_type> v;
for (auto& e : c)
{
concat(v, e);
}
return v;
}
int main()
{
vector<string> malwares1 = {"Malware1", "Malware2"};
vector<string> malwares2 = {"Malware3", "Malware4"};
vector<vector<string>> mVector = {malwares1, malwares2};
vector<string> malwares3 = to_vec_from_vectors(mVector);
return 0;
}
Upvotes: 2
Views: 71
Reputation: 20934
*std::begin(c)
returns vector<string>&
, you cannot store reference to vector in another vector, use remove_reference_t
to discard it:
template <typename Container>
inline
auto to_vec_from_vectors(Container& c) -> std::vector<typename std::remove_reference_t<decltype(*std::begin(c))>::value_type>
{
std::vector<typename std::remove_reference_t<decltype(*std::begin(c))>::value_type> v;
for (auto& e : c)
{
concat(v, e);
}
return v;
}
Upvotes: 2
Reputation: 63039
The type typename decltype(*std::begin(c))::value_type
seems to be what is giving you problems. typename Container::value_type::value_type
works for me.
#include <vector>
#include <string>
template <typename Container>
Container& concat(Container& c1, Container const& c2)
{
c1.insert(end(c1), begin(c2), end(c2));
return c1;
}
template <typename Container>
inline auto to_vec_from_vectors(Container& c) -> std::vector<typename Container::value_type::value_type>
{
std::vector<typename Container::value_type::value_type> v;
for (auto& e : c)
{
concat(v, e);
}
return v;
}
int main()
{
using std::vector;
using std::string;
vector<string> malwares1 = {"Malware1", "Malware2"};
vector<string> malwares2 = {"Malware3", "Malware4"};
vector<vector<string>> mVector = {malwares1, malwares2};
vector<string> malwares3 = to_vec_from_vectors(mVector);
return 0;
}
Upvotes: 3