c++编译错误(试图创建向量的静态向量)

C++ Compiler Error (trying to create a static vector of vectors)

本文关键字:向量 静态 创建 编译 错误 c++      更新时间:2023-10-16

我试图创建一个静态向量向量,我发现以下代码在gcc-4.1.2下编译和运行,但在gcc-4.5.1下它无法编译消息

assign.cxx:19:48: error: no matching function for call to ‘to(boost::assign_detail::generic_list<std::basic_string<char> >&)’

谁能解释一下为什么会这样?如果你对如何做我正在尝试做的事情有任何其他建议,那么我很乐意接受:)。

#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;
}

问题源于这样一个事实,即在to<>()的定义中,containerType被声明为只有一个模板参数,而实际上std::vector<>有两个模板参数。

这个在GCC 4.1.2中编译的事实只表明GCC 4.1.2中有一个错误——代码本质上仍然是不正确的。

不幸的是,我想不出一个好的解决方法。下面的代码编译,但将您限制为只有两个模板参数的容器(其中第二个是分配器),这可能不是您最终想要的:
#include <iostream>
#include <string>
#include <vector>
#include <boost/assign/list_of.hpp>
template<
  template<typename, typename> class containerType,
  typename allocatorType,
  typename elemType
>
containerType<elemType, allocatorType >
to(boost::assign_detail::generic_list<elemType> const& list)
{
  return list; // no need to explicitly construct a containerType instance
}
template<template<typename, typename> class containerType, typename elemType>
containerType<elemType, std::allocator<elemType> >
to(boost::assign_detail::generic_list<elemType> const& list)
{
  return to<containerType, std::allocator<elemType> >(list);
}
static 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()
{
  typedef std::vector<std::vector<std::string> >::const_iterator outer_iter_t;
  typedef std::vector<std::string>::const_iterator inner_iter_t;
  for (outer_iter_t itVec = names.begin(); itVec != names.end(); ++itVec)
    for (inner_iter_t itStr = itVec->begin(); itStr != itVec->end(); ++itStr)
      std::cout << "Value is " << *itStr << 'n';
}

EDIT(响应dribeas的评论):更新,允许调用者覆盖默认的分配器

在解决了几个问题之后,我认为最简单的解决方案不是使用模板模板参数,而是使用类型模板参数:

template <typename containerType, typename elemType>
containerType to( boost::assign_detail::generic_list<elemType> list ) {
  containerType tempContainer = list;
  return tempContainer;
}
static const std::vector<std::vector<std::string> > names = boost::assign::list_of
  (to<std::vector<std::string> >(boost::assign::list_of<std::string>("A")("B")("C") ))
  (to<std::vector<std::string> >(boost::assign::list_of<std::string>("D")("E")("F") ));

它可能需要在调用端输入更多的内容,但它会处理您没有在模板模板参数中声明的额外模板参数。

对函数to<std::vector>( ... )的调用只包含一个模板参数,而函数声明包含两个模板参数。我会尝试:to<std::vector, std::string>( ... )