I’m trying to create a static vector-of-vectors and I’m finding that the following code compiles and runs under gcc-4.1.2 but under gcc-4.5.1 it fails to compile with the message
assign.cxx:19:48: error: no matching function for call to ‘to(boost::assign_detail::generic_list<std::basic_string<char> >&)’
Can anyone explain why this happens? If you have any other suggestions about how to do what I’m trying to do then I’d be happy with that instead :).
#include <iostream>
#include <string>
#include <vector>
#include <boost/assign/list_of.hpp>
template <template <typename> class containerType, typename elemType>
containerType<elemType> to( boost::assign_detail::generic_list<elemType> list ) {
containerType<elemType> tempContainer = list;
return tempContainer;
}
static const std::vector<std::vector<std::string> > names = boost::assign::list_of
(to<std::vector>(boost::assign::list_of<std::string>("A")("B")("C") ))
(to<std::vector>(boost::assign::list_of<std::string>("D")("E")("F") ));
int main() {
for( std::vector< std::vector<std::string> >::const_iterator itVec = names.begin(); itVec != names.end(); ++itVec ) {
for( std::vector<std::string>::const_iterator itStr = itVec->begin(); itStr != itVec->end(); ++itStr ) {
std::cout << "Value is " << *itStr << std::endl;
}
}
return 0;
}
The problem stems from the fact that in your definition of
to<>(),containerTypeis declared to have only one template parameter, when in factstd::vector<>has two template parameters.The fact that this compiles in GCC 4.1.2 only indicates a bug in GCC 4.1.2 — the code is still inherently incorrect.
Unfortunately, I can’t think of a good workaround offhand. The following compiles but restricts you to containers with only two template arguments (wherein the second is the allocator), which may not be what you ultimately want:
EDIT (in response to dribeas’ comment): Updated to allow the caller to override the default allocator.